Skip to content

Commit

Permalink
Merge pull request #5 from TomCrypto/dev
Browse files Browse the repository at this point in the history
v0.5.0.
  • Loading branch information
TomCrypto committed Dec 20, 2014
2 parents 3f8df7a + 31667ef commit 65144c5
Show file tree
Hide file tree
Showing 27 changed files with 1,680 additions and 388 deletions.
106 changes: 68 additions & 38 deletions Library/Bar.cs
Expand Up @@ -10,7 +10,7 @@ namespace AntTweakBar
/// <summary>
/// An AntTweakBar bar, which holds a set of variables.
/// </summary>
public sealed class Bar : IEnumerable<Variable>, IDisposable
public sealed class Bar : IEnumerable<IVariable>, IDisposable
{
/// <summary>
/// The default label for unnamed bars.
Expand Down Expand Up @@ -71,39 +71,6 @@ public void SetDefinition(String def)

#region Customization

/// <summary>
/// Shows or hides a variable group in this bar.
/// </summary>
/// <param name="group">The name of the group to show or hide.</param>
/// <param name="visible">Whether the group should be visible.</param>
public void ShowGroup(String group, Boolean visible)
{
Tw.SetCurrentWindow(ParentContext.Identifier);
Tw.Define(String.Format("{0}/`{1}` visible={2}", ID, group, visible ? "true" : "false"));
}

/// <summary>
/// Opens or closes a variable group in this bar.
/// </summary>
/// <param name="group">The name of the group to open or close.</param>
/// <param name="opened">Whether the group should be open.</param>
public void OpenGroup(String group, Boolean opened)
{
Tw.SetCurrentWindow(ParentContext.Identifier);
Tw.Define(String.Format("{0}/`{1}` opened={2}", ID, group, opened ? "true" : "false"));
}

/// <summary>
/// Moves a variable group into another group.
/// </summary>
/// <param name="group">The name of the group to move.</param>
/// <param name="into">The name of the group to move it into.</param>
public void MoveGroup(String group, String into)
{
Tw.SetCurrentWindow(ParentContext.Identifier);
Tw.Define(String.Format("{0}/`{1}` group=`{2}`", ID, group, into));
}

/// <summary>
/// Gets or sets this bar's label.
/// </summary>
Expand Down Expand Up @@ -140,6 +107,15 @@ public byte Alpha
set { Tw.SetParam(Pointer, null, "alpha", value); }
}

/// <summary>
/// Gets or sets this bar's text color.
/// </summary>
public BarTextColor TextColor
{
get { return Tw.GetStringParam(Pointer, null, "text") == "dark" ? BarTextColor.Dark : BarTextColor.Light; }
set { Tw.SetParam(Pointer, null, "text", value == BarTextColor.Dark ? "dark" : "light"); }
}

/// <summary>
/// Gets or sets this bar's position.
/// </summary>
Expand All @@ -158,6 +134,42 @@ public Size Size
set { Tw.SetParam(Pointer, null, "size", value); }
}

/// <summary>
/// Gets or sets the width in pixels of this bar's value column. Set to zero for auto-fit.
/// </summary>
public Int32 ValueColumnWidth
{
get { return Math.Max(0, Tw.GetIntParam(Pointer, null, "valueswidth")[0]); }
set { Tw.SetParam(Pointer, null, "valueswidth", value == 0 ? "fit" : value.ToString()); }
}

/// <summary>
/// Gets or sets this bar's refresh rate in seconds.
/// </summary>
public Int32 RefreshRate
{
get { return Tw.GetIntParam(Pointer, null, "refresh")[0]; }
set { Tw.SetParam(Pointer, null, "refresh", value); }
}

/// <summary>
/// Gets or sets the alignment of buttons in this bar.
/// </summary>
public BarButtonAlignment ButtonAlignment
{
get { return (BarButtonAlignment)Enum.Parse(typeof(BarButtonAlignment), Tw.GetStringParam(Pointer, null, "buttonalign"), true); }
set { Tw.SetParam(Pointer, null, "buttonalign", value.ToString().ToLower()); }
}

/// <summary>
/// Gets or sets whether this bar is iconified.
/// </summary>
public Boolean Iconified
{
get { return Tw.GetBooleanParam(Pointer, null, "iconified"); }
set { Tw.SetParam(Pointer, null, "iconified", value); }
}

/// <summary>
/// Gets or sets whether this bar can be iconified by the user.
/// </summary>
Expand Down Expand Up @@ -203,6 +215,24 @@ public Boolean Visible
set { Tw.SetParam(Pointer, null, "visible", value); }
}

/// <summary>
/// Gets or sets whether this bar is always at the front.
/// </summary>
public Boolean AlwaysFront
{
get { return Tw.GetBooleanParam(Pointer, null, "alwaystop"); }
set { Tw.SetParam(Pointer, null, "alwaystop", value); }
}

/// <summary>
/// Gets or sets whether this bar is always at the back.
/// </summary>
public Boolean AlwaysBack
{
get { return Tw.GetBooleanParam(Pointer, null, "alwaysbottom"); }
set { Tw.SetParam(Pointer, null, "alwaysbottom", value); }
}

/// <summary>
/// Brings this bar in front of all others.
/// </summary>
Expand All @@ -223,14 +253,14 @@ public void SendToBack()

#region IEnumerable

private readonly ICollection<Variable> variables = new HashSet<Variable>();
private readonly ICollection<IVariable> variables = new HashSet<IVariable>();

internal void Add(Variable variable)
internal void Add(IVariable variable)
{
variables.Add(variable);
}

internal void Remove(Variable variable)
internal void Remove(IVariable variable)
{
variables.Remove(variable);
}
Expand All @@ -247,7 +277,7 @@ public void Clear()
}
}

public IEnumerator<Variable> GetEnumerator()
public IEnumerator<IVariable> GetEnumerator()
{
return variables.GetEnumerator();
}
Expand Down
2 changes: 1 addition & 1 deletion Library/BoolVariable.cs
Expand Up @@ -22,7 +22,7 @@ public BoolValidationEventArgs(bool value)
/// <summary>
/// An AntTweakBar variable which can hold a boolean value.
/// </summary>
public sealed class BoolVariable : Variable
public sealed class BoolVariable : Variable, IValueVariable
{
/// <summary>
/// Occurs when the user changes this variable's value.
Expand Down
2 changes: 1 addition & 1 deletion Library/Color4Variable.cs
Expand Up @@ -28,7 +28,7 @@ public Color4ValidationEventArgs(float r, float g, float b, float a)
/// <summary>
/// An AntTweakBar variable which can hold an RGBA color value.
/// </summary>
public sealed class Color4Variable : Variable
public sealed class Color4Variable : Variable, IValueVariable
{
/// <summary>
/// Occurs when the user changes this variable's value.
Expand Down
2 changes: 1 addition & 1 deletion Library/ColorVariable.cs
Expand Up @@ -26,7 +26,7 @@ public ColorValidationEventArgs(float r, float g, float b)
/// <summary>
/// An AntTweakBar variable which can hold an RGB color value.
/// </summary>
public sealed class ColorVariable : Variable
public sealed class ColorVariable : Variable, IValueVariable
{
/// <summary>
/// Occurs when the user changes this variable's value.
Expand Down
172 changes: 164 additions & 8 deletions Library/Context.cs
Expand Up @@ -139,25 +139,88 @@ public bool HandleMouseClick(Tw.MouseAction action, Tw.MouseButton button)
return Tw.MouseClick(action, button);
}

/* I don't know if this input handling code is correct. It is actually NOT straightforward to
* convert from conventional KeyDown/KeyPressed/KeyUp events to AntTweakBar key presses. This
* may or may not be correct and was inspired from TwEventSFML.cpp, if you know how to fix it
* please issue a pull request or a patch, until then it looks satisfactory. Kind of.
*/

private Tw.KeyModifiers lastModifiers;
private bool ignoreKeyPress;

/// <summary>
/// All the special keys supported by AntTweakBar.
/// </summary>
private static IList<Tw.Key> SpecialKeys = new List<Tw.Key>() {
Tw.Key.Escape, Tw.Key.Return, Tw.Key.Tab, Tw.Key.Backspace, Tw.Key.PageUp, Tw.Key.PageDown,
Tw.Key.Up, Tw.Key.Down, Tw.Key.Left, Tw.Key.Right, Tw.Key.End, Tw.Key.Home, Tw.Key.Insert,
Tw.Key.Delete, Tw.Key.Space, Tw.Key.F1, Tw.Key.F2, Tw.Key.F3, Tw.Key.F4, Tw.Key.F5,
Tw.Key.F6, Tw.Key.F7, Tw.Key.F8, Tw.Key.F9, Tw.Key.F10, Tw.Key.F11, Tw.Key.F12,
Tw.Key.F13, Tw.Key.F14, Tw.Key.F15,
};

/// <summary>
/// Notifies this context of a key press.
/// Notifies this context that a character has been typed.
/// </summary>
/// <param name="key">The key character pressed.</param>
public bool HandleKeyPress(char key)
/// <param name="character">The character typed.</param>
public bool HandleKeyPress(char character)
{
Tw.SetCurrentWindow(Identifier);
return Tw.KeyPressed((int)key, Tw.KeyModifier.None);

if (!ignoreKeyPress) {
if (((character & 0xFF) < 32) && (character != 0) && ((character & 0xFF00) == 0)) {
return Tw.KeyPressed((char)((character & 0xFF) + 'a' - 1), Tw.KeyModifiers.Ctrl | lastModifiers);
} else { // this is supposed to handle the Ctrl+letter combination properly (somehow)
return Tw.KeyPressed((char)(character & 0xFF), Tw.KeyModifiers.None);
}
} else {
ignoreKeyPress = false;
}

return false;
}

/// <summary>
/// Notifies this context of a special key press.
/// Notifies this context that a key has been pressed.
/// </summary>
/// <param name="key">The key pressed.</param>
/// <param name="modifiers">The key modifiers pressed.</param>
public bool HandleKeyPress(Tw.SpecialKey key, Tw.KeyModifier modifiers)
/// <param name="modifiers">The key modifiers.</param>
public bool HandleKeyDown(Tw.Key key, Tw.KeyModifiers modifiers)
{
Tw.SetCurrentWindow(Identifier);
return Tw.KeyPressed((int)key, modifiers);
lastModifiers = modifiers;

if (SpecialKeys.Contains(key)) {
ignoreKeyPress = true; // special keys
return Tw.KeyPressed(key, modifiers);
}

if (modifiers.HasFlag(Tw.KeyModifiers.Alt)) {
if ((key >= Tw.Key.A) && (key <= Tw.Key.Z)) {
ignoreKeyPress = true; // normal key shortcuts?
if (modifiers.HasFlag(Tw.KeyModifiers.Shift)) {
key = (Tw.Key)('A' + key - Tw.Key.A);
return Tw.KeyPressed(key, modifiers);
} else {
key = (Tw.Key)('a' + key - Tw.Key.A);
return Tw.KeyPressed(key, modifiers);
}
}
}

return false;
}

/// <summary>
/// Notifies this context that a key has been released.
/// </summary>
/// <param name="key">The key released.</param>
/// <param name="modifiers">The key modifiers.</param>
public bool HandleKeyUp(Tw.Key key, Tw.KeyModifiers modifiers)
{
lastModifiers = Tw.KeyModifiers.None;
ignoreKeyPress = false;
return false;
}

/// <summary>
Expand Down Expand Up @@ -201,6 +264,99 @@ public void ShowHelpBar(Boolean visible)
Tw.Define("TW_HELP visible=" + (visible ? "true" : "false"));
}

/// <summary>
/// Sets whether to draw or clip overlapping bars.
/// </summary>
/// <param name="overlap">If false, clips the contents of overlapping region (default).</param>
public void SetOverlap(Boolean overlap)
{
Tw.SetCurrentWindow(Identifier); // applies to this context
Tw.Define("GLOBAL overlap=" + (overlap ? "true" : "false"));
}

/// <summary>
/// Sets the icon position for all bars.
/// </summary>
/// <param name="position">The icon position (default is bottom left).</param>
public void SetIconPosition(BarIconPosition position)
{
Tw.SetCurrentWindow(Identifier);
Tw.Define("GLOBAL iconpos=" + position.ToString().ToLower());
}

/// <summary>
/// Sets the icon alignment for all bars.
/// </summary>
/// <param name="alignment">The icon alignment (default is vertical).</param>
public void SetIconAlignment(BarIconAlignment alignment)
{
Tw.SetCurrentWindow(Identifier);
Tw.Define("GLOBAL iconalign=" + alignment.ToString().ToLower());
}

/// <summary>
/// Sets the icon margin for all bars.
/// </summary>
/// <param name="margin">The icon margin as an x-y offset in pixels.</param>
public void SetIconMargin(Size margin)
{
Tw.SetCurrentWindow(Identifier);
Tw.Define(String.Format("GLOBAL iconmargin='{0} {1}'", margin.Width, margin.Height));
}

/// <summary>
/// Sets the font size for all bars. Note the fixed style font is not resizable.
/// </summary>
/// <param name="fontSize">The font size to use (default is medium).</param>
public void SetFontSize(BarFontSize fontSize)
{
int index = 0;

switch (fontSize) {
case BarFontSize.Small:
index = 1;
break;
case BarFontSize.Medium:
index = 2;
break;
case BarFontSize.Large:
index = 3;
break;
}

Tw.SetCurrentWindow(Identifier);
Tw.Define("GLOBAL fontsize=" + index);
}

/// <summary>
/// Sets the font style for all bars.
/// </summary>
/// <param name="fontStyle">The font style to use.</param>
public void SetFontStyle(BarFontStyle fontStyle)
{
Tw.SetCurrentWindow(Identifier);
Tw.Define("GLOBAL fontstyle=" + (fontStyle == BarFontStyle.Default ? "default" : "fixed"));
}

/// <summary>
/// Sets whether the bar font can be resized by the user.
/// </summary>
/// <param name="resizable">Whether the font can be resized.</param>
public void SetFontResizable(Boolean resizable)
{
Tw.SetCurrentWindow(Identifier);
Tw.Define("GLOBAL fontresizable=" + (resizable ? "true" : "false"));
}

/// <summary>
/// Sets a font scaling coefficient. Must be called once before initializing the first context.
/// </summary>
/// <param name="scale">The font scale to use.</param>
public static void SetFontScaling(Double scale)
{
Tw.Define("GLOBAL fontscaling=" + scale);
}

#endregion

#region IEnumerable
Expand Down
2 changes: 1 addition & 1 deletion Library/DoubleVariable.cs
Expand Up @@ -22,7 +22,7 @@ public DoubleValidationEventArgs(double value)
/// <summary>
/// An AntTweakBar variable which can hold a double-precision floating-point number.
/// </summary>
public sealed class DoubleVariable : Variable
public sealed class DoubleVariable : Variable, IValueVariable
{
/// <summary>
/// Occurs when the user changes this variable's value.
Expand Down

0 comments on commit 65144c5

Please sign in to comment.