Simplify ClickableScreen

This commit is contained in:
UnknownShadow200 2018-03-31 10:10:03 +11:00
parent 948167cd7c
commit 2676ea8b75
15 changed files with 62 additions and 111 deletions

View file

@ -33,10 +33,7 @@ namespace ClassicalSharp.Gui.Screens {
return false;
}
int lastX = -1, lastY = -1;
protected bool HandleMouseMove(Widget[] widgets, int mouseX, int mouseY) {
if (lastX == mouseX && lastY == mouseY)
return true;
protected int HandleMouseMove(Widget[] widgets, int mouseX, int mouseY) {
for (int i = 0; i < widgets.Length; i++) {
if (widgets[i] == null || !widgets[i].Active) continue;
widgets[i].Active = false;
@ -46,17 +43,10 @@ namespace ClassicalSharp.Gui.Screens {
Widget widget = widgets[i];
if (widget != null && widget.Bounds.Contains(mouseX, mouseY)) {
widget.Active = true;
lastX = mouseX; lastY = mouseY;
WidgetSelected(widget);
return true;
return i;
}
}
lastX = mouseX; lastY = mouseY;
WidgetSelected(null);
return false;
}
protected virtual void WidgetSelected(Widget widget) {
return -1;
}
protected ButtonWidget MakeBack(bool toGame, Font font, SimpleClickHandler onClick) {

View file

@ -113,7 +113,7 @@ namespace ClassicalSharp.Gui.Screens {
}
public override bool HandlesMouseMove(int mouseX, int mouseY) {
return HandleMouseMove(buttons, mouseX, mouseY);
return HandleMouseMove(buttons, mouseX, mouseY) >= 0;
}
public override bool HandlesMouseClick(int mouseX, int mouseY, MouseButton button) {

View file

@ -85,15 +85,11 @@ namespace ClassicalSharp.Gui.Screens {
}
void HandleFontChange() {
int selIndex = IndexOfWidget(selectedButton);
game.Events.RaiseChatFontChanged();
Recreate();
game.Gui.RefreshHud();
if (selIndex >= 0) {
selectedButton = (ButtonWidget)widgets[selIndex];
if (selectedButton != null) selectedButton.Active = true;
}
selectedI = -1;
HandlesMouseMove(game.Mouse.X, game.Mouse.Y);
}
void MakeValidators() {

View file

@ -83,15 +83,19 @@ namespace ClassicalSharp.Gui.Screens {
base.Dispose();
}
protected ButtonWidget selectedButton, activeButton;
protected override void WidgetSelected(Widget widget) {
ButtonWidget button = widget as ButtonWidget;
if (selectedButton == button || button == null ||
button == widgets[widgets.Length - 2]) return;
protected ButtonWidget activeButton;
protected int selectedI = -1;
selectedButton = button;
if (activeButton != null) return;
UpdateDescription(selectedButton);
public override bool HandlesMouseMove(int mouseX, int mouseY) {
int i = HandleMouseMove(widgets, mouseX, mouseY);
if (i == -1 || i == selectedI) return true;
if (descriptions == null || i >= descriptions.Length) return true;
selectedI = i;
if (activeButton == null) {
UpdateDescription((ButtonWidget)widgets[i]);
}
return true;
}
protected void UpdateDescription(ButtonWidget widget) {
@ -130,12 +134,10 @@ namespace ClassicalSharp.Gui.Screens {
}
void ShowExtendedHelp() {
bool canShow = input == null && selectedButton != null && descriptions != null;
if (!canShow) return;
if (input != null || descriptions == null) return;
if (selectedI < 0 || selectedI >= descriptions.Length) return;
int index = IndexOfWidget(selectedButton);
if (index < 0 || index >= descriptions.Length) return;
string[] desc = descriptions[index];
string[] desc = descriptions[selectedI];
if (desc == null) return;
MakeExtendedHelp(desc);
}

View file

@ -40,7 +40,7 @@ namespace ClassicalSharp.Gui.Screens {
MakeOpt(1, -100, "Use CPE", onClick, GetCPE, SetCPE),
MakeOpt(1, -50, "Use server textures", onClick, GetTexs, SetTexs),
TextWidget.Create(game, "&eButtons on the right require a client restart", regularFont)
TextWidget.Create(game, "&eButtons on the right require restarting game", regularFont)
.SetLocation(Anchor.Centre, Anchor.Centre, 0, 100),
MakeBack(false, titleFont, SwitchBack),
null, null,

View file

@ -11,15 +11,13 @@ namespace ClassicalSharp.Gui.Screens {
}
TextWidget descWidget;
string descText;
ButtonWidget selectedWidget;
int selectedI = -1;
public override void Render(double delta) {
RenderMenuBounds();
game.Graphics.Texturing = true;
RenderWidgets(widgets, delta);
if (descWidget != null)
descWidget.Render(delta);
if (descWidget != null) descWidget.Render(delta);
game.Graphics.Texturing = false;
}
@ -43,7 +41,9 @@ namespace ClassicalSharp.Gui.Screens {
MakeBack(false, titleFont, SwitchPause),
};
if (descWidget != null) MakeDescWidget(descText);
if (selectedI >= 0) {
MakeDescWidget(descriptions[selectedI]);
}
CheckHacksAllowed(null, null);
}
@ -60,23 +60,9 @@ namespace ClassicalSharp.Gui.Screens {
widgets[5].Disabled = !game.LocalPlayer.Hacks.CanAnyHacks; // env settings
}
protected override void WidgetSelected(Widget widget) {
ButtonWidget button = widget as ButtonWidget;
if (selectedWidget == widget || button == null ||
button == widgets[widgets.Length - 1]) return;
selectedWidget = button;
if (descWidget != null) descWidget.Dispose();
if (button == null) return;
string text = descriptions[IndexOfWidget(button)];
MakeDescWidget(text);
}
void MakeDescWidget(string text) {
descWidget = TextWidget.Create(game, text, regularFont)
.SetLocation(Anchor.Centre, Anchor.Centre, 0, 100);
descText = text;
}
ButtonWidget Make(int dir, int y, string text, SimpleClickHandler onClick) {
@ -86,14 +72,25 @@ namespace ClassicalSharp.Gui.Screens {
public override bool HandlesKeyDown(Key key) {
if (key == Key.Escape)
if (key == Key.Escape) {
game.Gui.SetNewScreen(null);
}
return key < Key.F1 || key > Key.F35;
}
public override bool HandlesMouseMove(int mouseX, int mouseY) {
int i = HandleMouseMove(widgets, mouseX, mouseY);
if (i == -1 || i == selectedI) return true;
if (i >= descriptions.Length) return true;
selectedI = i;
if (descWidget != null) descWidget.Dispose();
MakeDescWidget(descriptions[i]);
return true;
}
public override void OnResize(int width, int height) {
if (descWidget != null)
descWidget.Reposition();
if (descWidget != null) descWidget.Reposition();
base.OnResize(width, height);
}

View file

@ -53,7 +53,7 @@ namespace ClassicalSharp.Gui.Screens {
}
public override bool HandlesMouseMove(int mouseX, int mouseY) {
return HandleMouseMove(widgets, mouseX, mouseY);
return HandleMouseMove(widgets, mouseX, mouseY) >= 0;
}
public override bool HandlesMouseScroll(float delta) { return true; }

View file

@ -255,10 +255,6 @@ namespace ClassicalSharp.Network.Protocols {
const int bulkCount = 256;
unsafe void HandleBulkBlockUpdate() {
int count = reader.ReadUInt8() + 1;
if (!game.World.HasBlocks) {
reader.Skip(bulkCount * (sizeof(int) + 1)); return;
}
int* indices = stackalloc int[bulkCount];
for (int i = 0; i < count; i++) {
indices[i] = reader.ReadInt32();

View file

@ -181,7 +181,7 @@ namespace ClassicalSharp.Network.Protocols {
int y = reader.ReadUInt16();
int z = reader.ReadUInt16();
BlockID block = reader.ReadBlock();
if (game.World.HasBlocks && game.World.IsValidPos(x, y, z)) {
if (game.World.IsValidPos(x, y, z)) {
game.UpdateBlock(x, y, z, block);
}
}

View file

@ -12,8 +12,7 @@ namespace ClassicalSharp.Renderers {
Game game;
World map;
public int RainTexId, SnowTexId;
int rainTexId, snowTexId;
int vb;
public short[] heightmap;
@ -38,7 +37,7 @@ namespace ClassicalSharp.Renderers {
if (heightmap == null) InitHeightmap();
IGraphicsApi gfx = game.Graphics;
gfx.BindTexture(weather == Weather.Rainy ? RainTexId : SnowTexId);
gfx.BindTexture(weather == Weather.Rainy ? rainTexId : snowTexId);
Vector3 camPos = game.CurrentCameraPos;
Vector3I pos = Vector3I.Floor(camPos);
bool moved = pos != lastPos;
@ -127,14 +126,14 @@ namespace ClassicalSharp.Renderers {
void TextureChanged(object sender, TextureEventArgs e) {
if (e.Name == "snow.png")
game.UpdateTexture(ref SnowTexId, e.Name, e.Data, false);
game.UpdateTexture(ref snowTexId, e.Name, e.Data, false);
else if (e.Name == "rain.png")
game.UpdateTexture(ref RainTexId, e.Name, e.Data, false);
game.UpdateTexture(ref rainTexId, e.Name, e.Data, false);
}
public void Dispose() {
game.Graphics.DeleteTexture(ref RainTexId);
game.Graphics.DeleteTexture(ref SnowTexId);
game.Graphics.DeleteTexture(ref rainTexId);
game.Graphics.DeleteTexture(ref snowTexId);
ContextLost();
game.Events.TextureChanged -= TextureChanged;

View file

@ -36,14 +36,6 @@
#define Elem_HandlesMouseScroll(elem, delta) (elem)->VTABLE->HandlesMouseScroll((GuiElement*)(elem), delta)
typedef struct ClickableScreen_ {
GuiElement* Elem;
Widget** Widgets;
UInt32 WidgetsCount;
Int32 LastX, LastY;
void (*OnWidgetSelected)(GuiElement* elem, Widget* widget);
} ClickableScreen;
typedef struct InventoryScreen_ {
Screen_Layout
FontDesc Font;
@ -82,7 +74,6 @@ typedef struct FilesScreen_ {
ButtonWidget Buttons[FILES_SCREEN_BUTTONS];
TextWidget Title;
Widget* Widgets[FILES_SCREEN_BUTTONS + 1];
ClickableScreen Clickable;
StringsBuffer Entries; /* NOTE: this is the last member so we can avoid memsetting it to 0 */
} FilesScreen;
@ -194,8 +185,6 @@ void MenuScreen_RenderBounds(void) {
GfxCommon_Draw2DGradient(0, 0, Game_Width, Game_Height, topCol, bottomCol);
}
void ClickableScreen_DefaultWidgetSelected(GuiElement* elem, Widget* widget) { }
bool Clickable_HandleMouseDown(Widget** widgets, Int32 count, Int32 x, Int32 y, MouseButton btn) {
Int32 i;
/* iterate backwards (because last elements rendered are shown over others) */
@ -211,35 +200,21 @@ bool Clickable_HandleMouseDown(Widget** widgets, Int32 count, Int32 x, Int32 y,
return false;
}
bool ClickableScreen_HandleMouseMove(ClickableScreen* data, Int32 x, Int32 y) {
if (data->LastX == x && data->LastY == y) return true;
UInt32 i;
for (i = 0; i < data->WidgetsCount; i++) {
Widget* widget = data->Widgets[i];
Int32 Clickable_HandleMouseMove(Widget** widgets, Int32 count, Int32 x, Int32 y) {
Int32 i;
for (i = 0; i < count; i++) {
Widget* widget = widgets[i];
if (widget != NULL) widget->Active = false;
}
for (i = data->WidgetsCount; i > 0; i--) {
Widget* widget = data->Widgets[i];
for (i = count - 1; i >= 0; i--) {
Widget* widget = widgets[i];
if (widget != NULL && Widget_Contains(widget, x, y)) {
widget->Active = true;
data->LastX = x; data->LastY = y;
data->OnWidgetSelected(data->Elem, widget);
return true;
return i;
}
}
data->LastX = x; data->LastY = y;
data->OnWidgetSelected(data->Elem, NULL);
return false;
}
void ClickableScreen_Init(ClickableScreen* data, GuiElement* elem, Widget** widgets, UInt32 widgetsCount) {
data->Elem = elem;
data->Widgets = widgets;
data->WidgetsCount = widgetsCount;
data->LastX = -1; data->LastY = -1;
data->OnWidgetSelected = ClickableScreen_DefaultWidgetSelected;
return -1;
}
@ -658,7 +633,6 @@ void FilesScreen_ContextRecreated(void* obj) {
for (i = 0; i < FILES_SCREEN_BUTTONS; i++) {
screen->Widgets[i + 1] = (Widget*)(&screen->Buttons[i]);
}
ClickableScreen_Init(&screen->Clickable, (GuiElement*)screen, screen->Widgets, Array_Elems(screen->Widgets));
FilesScreen_UpdateArrows(screen);
}
@ -702,7 +676,7 @@ bool FilesScreen_HandlesKeyDown(GuiElement* elem, Key key) {
bool FilesScreen_HandlesMouseMove(GuiElement* elem, Int32 x, Int32 y) {
FilesScreen* screen = (FilesScreen*)elem;
return ClickableScreen_HandleMouseMove(&screen->Clickable, x, y);
return Clickable_HandleMouseMove(screen->Widgets, Array_Elems(screen->Widgets), x, y) >= 0;
}
bool FilesScreen_HandlesMouseDown(GuiElement* elem, Int32 x, Int32 y, MouseButton btn) {

View file

@ -1,6 +1,5 @@
#ifndef CC_SERVERCONNECTION_H
#define CC_SERVERCONNECTION_H
#include "String.h"
#include "Input.h"
#include "GameStructs.h"
#include "Picking.h"

View file

@ -6,7 +6,6 @@
Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
*/
/* Number of elements in each row of 2D atlas. */
#define ATLAS2D_ELEMENTS_PER_ROW 16
/* Number of rows in 2D atlas. */

View file

@ -55,12 +55,11 @@ BlockID World_GetPhysicsBlock(Int32 x, Int32 y, Int32 z) {
if (x < 0 || x >= World_Width || z < 0 || z >= World_Length || y < 0) return BLOCK_BEDROCK;
if (y >= World_Height) return BLOCK_AIR;
return World_Blocks[World_Pack(x, y, z)];
return World_GetBlock(x, y, z);
}
BlockID World_SafeGetBlock_3I(Vector3I p) {
return World_IsValidPos(p.X, p.Y, p.Z) ?
World_Blocks[World_Pack(p.X, p.Y, p.Z)] : BLOCK_AIR;
return World_IsValidPos(p.X, p.Y, p.Z) ? World_GetBlock(p.X, p.Y, p.Z) : BLOCK_AIR;
}
bool World_IsValidPos(Int32 x, Int32 y, Int32 z) {