simplify code a little bit

This commit is contained in:
UnknownShadow200 2018-04-08 13:20:58 +10:00
parent ad75bf435e
commit ebe092b818
6 changed files with 40 additions and 64 deletions

View file

@ -66,7 +66,7 @@ namespace ClassicalSharp.Gui {
public Widget(Game game) : base(game) { }
public ClickHandler MenuClick;
public ClickHandler MenuClick;
public bool Active, Disabled;
public int X, Y, Width, Height;
public Anchor HorizontalAnchor, VerticalAnchor;

View file

@ -14,8 +14,7 @@ namespace ClassicalSharp.Gui.Screens {
MenuInputWidget selected;
public override bool HandlesKeyPress(char key) {
return selected == null ? true :
selected.HandlesKeyPress(key);
return selected == null || selected.HandlesKeyPress(key);
}
public override bool HandlesKeyDown(Key key) {
@ -24,7 +23,7 @@ namespace ClassicalSharp.Gui.Screens {
}
public override bool HandlesKeyUp(Key key) {
return selected == null ? true : selected.HandlesKeyUp(key);
return selected == null || selected.HandlesKeyUp(key);
}
public override void Init() {
@ -40,8 +39,10 @@ namespace ClassicalSharp.Gui.Screens {
MakeInput(0, false, game.World.Length.ToString()),
MakeInput(40, true, ""),
MakeLabel(-150, -80, "Width:"), MakeLabel(-150, -40, "Height:"),
MakeLabel(-150, 0, "Length:"), MakeLabel(-140, 40, "Seed:"),
MakeLabel(-150, -80, "Width:"),
MakeLabel(-150, -40, "Height:"),
MakeLabel(-150, 0, "Length:"),
MakeLabel(-140, 40, "Seed:"),
TextWidget.Create(game, "Generate new level", textFont)
.SetLocation(Anchor.Centre, Anchor.Centre, 0, -130),
@ -55,11 +56,9 @@ namespace ClassicalSharp.Gui.Screens {
InputWidget MakeInput(int y, bool seed, string value) {
MenuInputValidator validator = seed ? new SeedValidator() : new IntegerValidator(1, 8192);
InputWidget input = MenuInputWidget.Create(game, 200, 30, value,
textFont, validator)
InputWidget input = MenuInputWidget.Create(game, 200, 30, value, textFont, validator)
.SetLocation(Anchor.Centre, Anchor.Centre, 0, y);
input.Active = false;
input.MenuClick = InputClick;
return input;
}
@ -112,9 +111,9 @@ namespace ClassicalSharp.Gui.Screens {
int GetInt(int index) {
MenuInputWidget input = (MenuInputWidget)widgets[index];
string text = input.Text.ToString();
if (!input.Validator.IsValidValue(text))
return 0;
return text == "" ? 0 : Int32.Parse(text);
if (!input.Validator.IsValidValue(text)) return 0;
return Int32.Parse(text);
}
int GetSeedInt(int index) {
@ -122,9 +121,8 @@ namespace ClassicalSharp.Gui.Screens {
string text = input.Text.ToString();
if (text == "") return new Random().Next();
if (!input.Validator.IsValidValue(text))
return 0;
return text == "" ? 0 : Int32.Parse(text);
if (!input.Validator.IsValidValue(text)) return 0;
return Int32.Parse(text);
}
}

View file

@ -40,27 +40,12 @@ namespace OpenTK.Platform.Windows {
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto), SuppressUnmanagedCodeSecurity]
internal static extern short UnregisterClass(IntPtr className, IntPtr instance);
internal static IntPtr SetWindowLong_N(IntPtr handle, GetWindowLongOffsets item, IntPtr newValue) {
return IntPtr.Size == 4 ? (IntPtr)SetWindowLong(handle, item, newValue.ToInt32()) :
SetWindowLongPtr(handle, item, newValue);
}
[DllImport("user32.dll", SetLastError = true), SuppressUnmanagedCodeSecurity]
static extern int SetWindowLong(IntPtr hWnd, GetWindowLongOffsets nIndex, int dwNewLong);
internal static extern int SetWindowLong(IntPtr hWnd, GetWindowLongOffsets nIndex, int dwNewLong);
[DllImport("user32.dll", SetLastError = true), SuppressUnmanagedCodeSecurity]
static extern IntPtr SetWindowLongPtr(IntPtr hWnd, GetWindowLongOffsets nIndex, IntPtr dwNewLong);
internal static UIntPtr GetWindowLong_N(IntPtr handle, GetWindowLongOffsets index) {
return IntPtr.Size == 4 ? (UIntPtr)GetWindowLong(handle, index) : GetWindowLongPtr(handle, index);
}
[DllImport("user32.dll", SetLastError = true), SuppressUnmanagedCodeSecurity]
static extern uint GetWindowLong(IntPtr hWnd, GetWindowLongOffsets nIndex);
[DllImport("user32.dll", SetLastError = true), SuppressUnmanagedCodeSecurity]
static extern UIntPtr GetWindowLongPtr(IntPtr hWnd, GetWindowLongOffsets nIndex);
internal static extern uint GetWindowLong(IntPtr hWnd, GetWindowLongOffsets nIndex);
[DllImport("user32.dll", SetLastError = true), SuppressUnmanagedCodeSecurity]
internal static extern IntPtr GetForegroundWindow();

View file

@ -86,10 +86,7 @@ namespace OpenTK.Platform.Windows
// See http://msdn.microsoft.com/en-us/library/ms646274(VS.85).aspx (WM_ACTIVATE notification):
// wParam: The low-order word specifies whether the window is being activated or deactivated.
bool new_focused_state = Focused;
if (IntPtr.Size == 4)
focused = (wParam.ToInt32() & 0xFFFF) != 0;
else
focused = (wParam.ToInt64() & 0xFFFF) != 0;
focused = (wParam.ToInt64() & 0xFFFF) != 0;
if (new_focused_state != Focused && FocusedChanged != null)
FocusedChanged(this, EventArgs.Empty);
@ -166,10 +163,7 @@ namespace OpenTK.Platform.Windows
#region Input events
case WindowMessage.CHAR:
if (IntPtr.Size == 4)
key_press.KeyChar = (char)wParam.ToInt32();
else
key_press.KeyChar = (char)wParam.ToInt64();
key_press.KeyChar = (char)wParam.ToInt64();
if (KeyPress != null)
KeyPress(this, key_press);
@ -541,7 +535,7 @@ namespace OpenTK.Platform.Windows
public Size ClientSize {
get { return ClientRectangle.Size; }
set {
WindowStyle style = (WindowStyle)API.GetWindowLong_N(window.handle, GetWindowLongOffsets.STYLE);
WindowStyle style = (WindowStyle)API.GetWindowLong(window.handle, GetWindowLongOffsets.STYLE);
Win32Rectangle rect = Win32Rectangle.From(value);
API.AdjustWindowRect(ref rect, style, false);
Size = new Size(rect.Width, rect.Height);
@ -670,7 +664,7 @@ namespace OpenTK.Platform.Windows
if( was_visible )
Visible = false;
API.SetWindowLong_N(window.handle, GetWindowLongOffsets.STYLE, (IntPtr)(int)style);
API.SetWindowLong(window.handle, GetWindowLongOffsets.STYLE, (int)style);
API.SetWindowPos(window.handle, IntPtr.Zero, 0, 0, rect.Width, rect.Height,
SetWindowPosFlags.NOMOVE | SetWindowPosFlags.NOZORDER |
SetWindowPosFlags.FRAMECHANGED);

View file

@ -753,6 +753,22 @@ void EditHotkeyScreen_MakeLeaveOpen(EditHotkeyScreen* screen, Widget_LeftClick o
EditHotkeyScreen_Make(screen, 2, -100, 10, &text, onClick);
}
void EditHotkeyScreen_BaseKey(GuiElement* elem, GuiElement* widget) {
EditHotkeyScreen* screen = (EditHotkeyScreen*)elem;
screen->SelectedI = 0;
screen->SupressNextPress = true;
String msg = String_FromConst("Key: press a key..");
ButtonWidget_SetText(&screen->Buttons[0], &msg);
}
void EditHotkeyScreen_Modifiers(GuiElement* elem, GuiElement* widget) {
EditHotkeyScreen* screen = (EditHotkeyScreen*)elem;
screen->SelectedI = 1;
screen->SupressNextPress = true;
String msg = String_FromConst("Modifiers: press a key..");
ButtonWidget_SetText(&screen->Buttons[1], &msg);
}
void EditHotkeyScreen_LeaveOpen(GuiElement* elem, GuiElement* widget) {
EditHotkeyScreen* screen = (EditHotkeyScreen*)elem;
/* Reset 'waiting for key..' state of two other buttons */
@ -796,24 +812,8 @@ void EditHotkeyScreen_RemoveHotkey(GuiElement* elem, GuiElement* widget) {
Gui_SetNewScreen(HotkeyListScreen_MakeInstance());
}
void EditHotkeyScreen_BaseKey(GuiElement* elem, GuiElement* widget) {
EditHotkeyScreen* screen = (EditHotkeyScreen*)elem;
screen->SelectedI = 0;
screen->SupressNextPress = true;
String msg = String_FromConst("Key: press a key..");
ButtonWidget_SetText(&screen->Buttons[0], &msg);
}
void EditHotkeyScreen_Modifiers(GuiElement* elem, GuiElement* widget) {
EditHotkeyScreen* screen = (EditHotkeyScreen*)elem;
screen->SelectedI = 1;
screen->SupressNextPress = true;
String msg = String_FromConst("Modifiers: press a key..");
ButtonWidget_SetText(&screen->Buttons[1], &msg);
}
void EditHotkeyScreen_Init(GuiElement* elem) {
EditHotkeyScreen* screen = (EditHotkeyScreen*)screen;
EditHotkeyScreen* screen = (EditHotkeyScreen*)elem;
MenuScreen_Init(elem);
Key_KeyRepeat = true;
screen->ContextRecreated(elem);
@ -835,7 +835,7 @@ void EditHotkeyScreen_Free(GuiElement* elem) {
}
bool EditHotkeyScreen_HandlesKeyPress(GuiElement* elem, UInt8 key) {
EditHotkeyScreen* screen = (EditHotkeyScreen*)screen;
EditHotkeyScreen* screen = (EditHotkeyScreen*)elem;
if (screen->SupressNextPress) {
screen->SupressNextPress = false;
return true;
@ -844,7 +844,7 @@ bool EditHotkeyScreen_HandlesKeyPress(GuiElement* elem, UInt8 key) {
}
bool EditHotkeyScreen_HandlesKeyDown(GuiElement* elem, Key key) {
EditHotkeyScreen* screen = (EditHotkeyScreen*)screen;
EditHotkeyScreen* screen = (EditHotkeyScreen*)elem;
if (screen->SelectedI >= 0) {
if (screen->SelectedI == 0) {
screen->CurHotkey.BaseKey = key;
@ -866,7 +866,7 @@ bool EditHotkeyScreen_HandlesKeyDown(GuiElement* elem, Key key) {
}
bool EditHotkeyScreen_HandlesKeyUp(GuiElement* elem, Key key) {
EditHotkeyScreen* screen = (EditHotkeyScreen*)screen;
EditHotkeyScreen* screen = (EditHotkeyScreen*)elem;
return Elem_HandlesKeyUp(&screen->Input.Base, key);
}

View file

@ -98,8 +98,7 @@ void Window_SetHiddenBorder(bool hidden) {
}
void Window_EnableMouseTracking(void) {
TRACKMOUSEEVENT me;
Platform_MemSet(&me, 0, sizeof(TRACKMOUSEEVENT));
TRACKMOUSEEVENT me = { 0 };
me.cbSize = sizeof(TRACKMOUSEEVENT);
me.hwndTrack = win_Handle;
me.dwFlags = TME_LEAVE;