Leadwerks GUI
Leadwerks GUI is now functioning, on the beta branch, Windows only, Lua interpreter only.
GUI Class
static GUI* Create(Context* context)
Creates a new GUI
Widget Class
static Widget* Create(const int x, const int y, const int width, const int height, Widget* parent, const int style=0)
Creates a new Widget. Widgets can be made into buttons, dropdown boxes, or anything else by attaching a script.
virtual bool SetScript(const std::string& path, const bool start = true)
Sets a widget's script for drawing and logic
virtual void SetText(const std::string& text)
Sets a text string for the widget that can be retrieved with GetText().
The widget script will call GUI drawing commands:
virtual void DrawImage(Image* image, const int x, const int y);//lua
virtual void DrawImage(Image* image, const int x, const int y, const int width, const int height);//lua
virtual void SetColor(const float r, const float g, const float b);//lua
virtual void SetColor(const float r, const float g, const float b, const float a);//lua
virtual void DrawRect(const int x, const int y, const int width, const int height, const int fillmode = 0, const int radius = 0);//lua
virtual void DrawText(std::string& text, const int x, const int y, const int width, const int height, const int style = 0);//lua
virtual void DrawLine(const int x0, const int y0, const int x1, const int y1);//lua
Images are also supported (these are loaded from a tex file):
static Image* Load(std::string& path, GUI* gui);
Images are a little funny. If the GUI is context-based it will internally use a texture. If the GUI is window-based the image will store a bitmap that varies with the operating system. The point is, the GUI drawing commands will work the same on either.
The button script looks like this and provides a fully functional button:
Script.pushed=false Script.hovered=false function Script:Draw() --System:Print("Paint Button") local pos = self.widget:GetPosition(true) local gui = self.widget:GetGUI() gui:SetColor(1,1,1,1) if self.pushed then gui:SetColor(0.2,0.2,0.2) else if self.hovered then gui:SetColor(0.3,0.3,0.3) else gui:SetColor(0.25,0.25,0.25) end end gui:DrawRect(pos.x,pos.y,self.widget.size.width,self.widget.size.height,0,3) gui:SetColor(0.9,0.9,0.9) local text = self.widget:GetText() if text~="" then if self.pushed then gui:DrawText(text,pos.x+1,pos.y+1,self.widget.size.width,self.widget.size.height,Text.Center+Text.VCenter) else gui:DrawText(text,pos.x,pos.y,self.widget.size.width,self.widget.size.height,Text.Center+Text.VCenter) end end gui:DrawRect(pos.x,pos.y,self.widget.size.width,self.widget.size.height,1,3) end function Script:MouseEnter(x,y) self.hovered = true self.widget:Redraw() end function Script:MouseLeave(x,y) self.hovered = false self.widget:Redraw() end function Script:MouseMove(x,y) --System:Print("MouseMove") end function Script:MouseDown(button,x,y) --System:Print("MouseDown") self.pushed=true self.widget:Redraw() end function Script:MouseUp(button,x,y) --System:Print("MouseUp") local gui = self.widget:GetGUI() self.pushed=false if self.hovered then EventQueue:Emit(Event.WidgetAction,self.widget) end self.widget:Redraw() end function Script:KeyDown(button,x,y) --System:Print("KeyDown") end function Script:KeyUp(button,x,y) --System:Print("KeyUp") end
The button uses the new EventQueue class to emit an event:
EventQueue:Emit(Event.WidgetAction,self.widget)
The main script can then poll events and find out when the button is pushed. This code is inserted into the main loop to do that:
while EventQueue:Peek() do local event = EventQueue:Wait() if event.id == Event.WidgetAction then if event.source == button then System:Print("The button was pressed!") end end end
Support for event callbacks will also be added.
The full main script to create a GUI and handle events looks like this:
--Initialize Steamworks (optional) Steamworks:Initialize() --Set the application title title="$PROJECT_TITLE" --Create a window local windowstyle = window.Titlebar + window.Resizable-- + window.Hidden if System:GetProperty("fullscreen")=="1" then windowstyle=windowstyle+window.FullScreen end window=Window:Create(title,0,0,System:GetProperty("screenwidth","1024"),System:GetProperty("screenheight","768"),windowstyle) --window:HideMouse() --Create the graphics context context=Context:Create(window) if context==nil then return end --Create a GUI local gui = GUI:Create(context) --Create a new widget local button = Widget:Create(20,20,300,50,gui:GetBase()) --Set the widget's script to make it a button button:SetScript("Scripts/GUI/Button.lua") --Set the button text button:SetText("Button") --Create a world world=World:Create() world:SetLightQuality((System:GetProperty("lightquality","1"))) --Load a map local mapfile = System:GetProperty("map","Maps/start.map") if Map:Load(mapfile)==false then return end --window:Show() while window:KeyDown(Key.Escape)==false do --Process events while EventQueue:Peek() do local event = EventQueue:Wait() if event.id == Event.WidgetAction then if event.source == button then System:Print("The button was pressed!") end end end --If window has been closed, end the program if window:Closed() then break end --Handle map change if changemapname~=nil then --Clear all entities world:Clear() --Load the next map Time:Pause() if Map:Load("Maps/"..changemapname..".map")==false then return end Time:Resume() changemapname = nil end --Update the app timing Time:Update() --Update the world world:Update() --Render the world world:Render() --Render statistics context:SetBlendMode(Blend.Alpha) if DEBUG then context:SetColor(1,0,0,1) context:DrawText("Debug Mode",2,2) context:SetColor(1,1,1,1) context:DrawStats(2,22) context:SetBlendMode(Blend.Solid) else --Toggle statistics on and off if (window:KeyHit(Key.F11)) then showstats = not showstats end if showstats then context:SetColor(1,1,1,1) context:DrawText("FPS: "..Math:Round(Time:UPS()),2,2) end end --Refresh the screen context:Sync(true) end
At this point the system contains everything you need to begin writing your own widget scripts. Small changes may occur in the API before the feature is finalized, but this is pretty close to the final product.
- 1
- 8
6 Comments
Recommended Comments