Created
November 11, 2023 12:29
-
-
Save justdan96/4abb9acacc8b81f1aab1509bee55976a to your computer and use it in GitHub Desktop.
PixelUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| // Property - GUI property | |
| type Property int32 | |
| // GUI properties enumeration | |
| const ( | |
| GlobalBaseColor Property = iota | |
| GlobalBorderColor | |
| GlobalTextColor | |
| GlobalTextFontsize | |
| GlobalBorderWidth | |
| GlobalBackgroundColor | |
| GlobalLinesColor | |
| LabelBorderWidth | |
| LabelTextColor | |
| LabelTextPadding | |
| ButtonBorderWidth | |
| ButtonTextPadding | |
| ButtonDefaultBorderColor | |
| ButtonDefaultInsideColor | |
| ButtonDefaultTextColor | |
| ButtonHoverBorderColor | |
| ButtonHoverInsideColor | |
| ButtonHoverTextColor | |
| ButtonPressedBorderColor | |
| ButtonPressedInsideColor | |
| ButtonPressedTextColor | |
| ToggleTextPadding | |
| ToggleBorderWidth | |
| ToggleDefaultBorderColor | |
| ToggleDefaultInsideColor | |
| ToggleDefaultTextColor | |
| ToggleHoverBorderColor | |
| ToggleHoverInsideColor | |
| ToggleHoverTextColor | |
| TogglePressedBorderColor | |
| TogglePressedInsideColor | |
| TogglePressedTextColor | |
| ToggleActiveBorderColor | |
| ToggleActiveInsideColor | |
| ToggleActiveTextColor | |
| TogglegroupPadding | |
| SliderBorderWidth | |
| SliderButtonBorderWidth | |
| SliderBorderColor | |
| SliderInsideColor | |
| SliderDefaultColor | |
| SliderHoverColor | |
| SliderActiveColor | |
| SliderbarBorderColor | |
| SliderbarInsideColor | |
| SliderbarDefaultColor | |
| SliderbarHoverColor | |
| SliderbarActiveColor | |
| SliderbarZeroLineColor | |
| ProgressbarBorderColor | |
| ProgressbarInsideColor | |
| ProgressbarProgressColor | |
| ProgressbarBorderWidth | |
| SpinnerLabelBorderColor | |
| SpinnerLabelInsideColor | |
| SpinnerDefaultButtonBorderColor | |
| SpinnerDefaultButtonInsideColor | |
| SpinnerDefaultSymbolColor | |
| SpinnerDefaultTextColor | |
| SpinnerHoverButtonBorderColor | |
| SpinnerHoverButtonInsideColor | |
| SpinnerHoverSymbolColor | |
| SpinnerHoverTextColor | |
| SpinnerPressedButtonBorderColor | |
| SpinnerPressedButtonInsideColor | |
| SpinnerPressedSymbolColor | |
| SpinnerPressedTextColor | |
| ComboboxPadding | |
| boundsWidth | |
| boundsHeight | |
| ComboboxBorderWidth | |
| ComboboxDefaultBorderColor | |
| ComboboxDefaultInsideColor | |
| ComboboxDefaultTextColor | |
| ComboboxDefaultListTextColor | |
| ComboboxHoverBorderColor | |
| ComboboxHoverInsideColor | |
| ComboboxHoverTextColor | |
| ComboboxHoverListTextColor | |
| ComboboxPressedBorderColor | |
| ComboboxPressedInsideColor | |
| ComboboxPressedTextColor | |
| ComboboxPressedListBorderColor | |
| ComboboxPressedListInsideColor | |
| ComboboxPressedListTextColor | |
| CheckboxDefaultBorderColor | |
| CheckboxDefaultInsideColor | |
| CheckboxHoverBorderColor | |
| CheckboxHoverInsideColor | |
| CheckboxClickBorderColor | |
| CheckboxClickInsideColor | |
| CheckboxDefaultActiveColor | |
| CheckboxInsideWidth | |
| TextboxBorderWidth | |
| TextboxBorderColor | |
| TextboxInsideColor | |
| TextboxTextColor | |
| TextboxLineColor | |
| TextboxTextFontsize | |
| ) | |
| // GUI controls states | |
| const ( | |
| Disabled = iota | |
| Normal | |
| Focused | |
| Pressed | |
| ) | |
| // Current GUI style (default light) | |
| var style = []int64{ | |
| 0xf5f5f5ff, // GLOBAL_BASE_COLOR | |
| 0xf5f5f5ff, // GLOBAL_BORDER_COLOR | |
| 0x3f3f3fff, // GLOBAL_TEXT_COLOR | |
| 10, // GLOBAL_TEXT_FONTSIZE | |
| 1, // GLOBAL_BORDER_WIDTH | |
| 0xcbcbcbff, // BACKGROUND_COLOR | |
| 0x90abb5ff, // LINES_COLOR | |
| 1, // LABEL_BORDER_WIDTH | |
| 0x4d4d4dff, // LABEL_TEXT_COLOR | |
| 20, // LABEL_TEXT_PADDING | |
| 5, // BUTTON_BORDER_WIDTH | |
| 4, // BUTTON_TEXT_PADDING | |
| 0x828282ff, // BUTTON_DEFAULT_BORDER_COLOR | |
| 0xafafafff, // BUTTON_DEFAULT_INSIDE_COLOR | |
| 0x3f3f3fff, // BUTTON_DEFAULT_TEXT_COLOR | |
| 0xc8c8c8ff, // BUTTON_HOVER_BORDER_COLOR | |
| 0xffffffff, // BUTTON_HOVER_INSIDE_COLOR | |
| 0x868686ff, // BUTTON_HOVER_TEXT_COLOR | |
| 0x7bb0d6ff, // BUTTON_PRESSED_BORDER_COLOR | |
| 0x9f9f9fff, // BUTTON_PRESSED_INSIDE_COLOR | |
| 0x5f9aa7ff, // BUTTON_PRESSED_TEXT_COLOR | |
| 20, // TOGGLE_TEXT_PADDING | |
| 1, // TOGGLE_BORDER_WIDTH | |
| 0x828282ff, // TOGGLE_DEFAULT_BORDER_COLOR | |
| 0xc8c8c8ff, // TOGGLE_DEFAULT_INSIDE_COLOR | |
| 0x828282ff, // TOGGLE_DEFAULT_TEXT_COLOR | |
| 0xc8c8c8ff, // TOGGLE_HOVER_BORDER_COLOR | |
| 0xffffffff, // TOGGLE_HOVER_INSIDE_COLOR | |
| 0x828282ff, // TOGGLE_HOVER_TEXT_COLOR | |
| 0xbdd7eaff, // TOGGLE_PRESSED_BORDER_COLOR | |
| 0xddf5ffff, // TOGGLE_PRESSED_INSIDE_COLOR | |
| 0xafccd3ff, // TOGGLE_PRESSED_TEXT_COLOR | |
| 0x7bb0d6ff, // TOGGLE_ACTIVE_BORDER_COLOR | |
| 0xbcecffff, // TOGGLE_ACTIVE_INSIDE_COLOR | |
| 0x5f9aa7ff, // TOGGLE_ACTIVE_TEXT_COLOR | |
| 3, // TOGGLEGROUP_PADDING | |
| 1, // SLIDER_BORDER_WIDTH | |
| 1, // SLIDER_BUTTON_BORDER_WIDTH | |
| 0x828282ff, // SLIDER_BORDER_COLOR | |
| 0xc8c8c8ff, // SLIDER_INSIDE_COLOR | |
| 0xbcecffff, // SLIDER_DEFAULT_COLOR | |
| 0xffffffff, // SLIDER_HOVER_COLOR | |
| 0xddf5ffff, // SLIDER_ACTIVE_COLOR | |
| 0x828282ff, // SLIDERBAR_BORDER_COLOR | |
| 0xc8c8c8ff, // SLIDERBAR_INSIDE_COLOR | |
| 0xbcecffff, // SLIDERBAR_DEFAULT_COLOR | |
| 0xffffffff, // SLIDERBAR_HOVER_COLOR | |
| 0xddf5ffff, // SLIDERBAR_ACTIVE_COLOR | |
| 0x828282ff, // SLIDERBAR_ZERO_LINE_COLOR | |
| 0x828282ff, // PROGRESSBAR_BORDER_COLOR | |
| 0xc8c8c8ff, // PROGRESSBAR_INSIDE_COLOR | |
| 0xbcecffff, // PROGRESSBAR_PROGRESS_COLOR | |
| 2, // PROGRESSBAR_BORDER_WIDTH | |
| 0x828282ff, // SPINNER_LABEL_BORDER_COLOR | |
| 0xc8c8c8ff, // SPINNER_LABEL_INSIDE_COLOR | |
| 0x828282ff, // SPINNER_DEFAULT_BUTTON_BORDER_COLOR | |
| 0xc8c8c8ff, // SPINNER_DEFAULT_BUTTON_INSIDE_COLOR | |
| 0x000000ff, // SPINNER_DEFAULT_SYMBOL_COLOR | |
| 0x000000ff, // SPINNER_DEFAULT_TEXT_COLOR | |
| 0xc8c8c8ff, // SPINNER_HOVER_BUTTON_BORDER_COLOR | |
| 0xffffffff, // SPINNER_HOVER_BUTTON_INSIDE_COLOR | |
| 0x000000ff, // SPINNER_HOVER_SYMBOL_COLOR | |
| 0x000000ff, // SPINNER_HOVER_TEXT_COLOR | |
| 0x7bb0d6ff, // SPINNER_PRESSED_BUTTON_BORDER_COLOR | |
| 0xbcecffff, // SPINNER_PRESSED_BUTTON_INSIDE_COLOR | |
| 0x5f9aa7ff, // SPINNER_PRESSED_SYMBOL_COLOR | |
| 0x000000ff, // SPINNER_PRESSED_TEXT_COLOR | |
| 3, // COMBOBOX_PADDING | |
| 3, // COMBOBOX_BUTTON_WIDTH | |
| 20, // COMBOBOX_BUTTON_HEIGHT | |
| 5, // COMBOBOX_BORDER_WIDTH | |
| 0x828282ff, // COMBOBOX_DEFAULT_BORDER_COLOR | |
| 0xc8c8c8ff, // COMBOBOX_DEFAULT_INSIDE_COLOR | |
| 0x828282ff, // COMBOBOX_DEFAULT_TEXT_COLOR | |
| 0x828282ff, // COMBOBOX_DEFAULT_LIST_TEXT_COLOR | |
| 0xc8c8c8ff, // COMBOBOX_HOVER_BORDER_COLOR | |
| 0xffffffff, // COMBOBOX_HOVER_INSIDE_COLOR | |
| 0x828282ff, // COMBOBOX_HOVER_TEXT_COLOR | |
| 0x828282ff, // COMBOBOX_HOVER_LIST_TEXT_COLOR | |
| 0x7bb0d6ff, // COMBOBOX_PRESSED_BORDER_COLOR | |
| 0x3f3f3fff, // COMBOBOX_PRESSED_INSIDE_COLOR | |
| 0x5f9aa7ff, // COMBOBOX_PRESSED_TEXT_COLOR | |
| 0x0078acff, // COMBOBOX_PRESSED_LIST_BORDER_COLOR | |
| 0x66e7ffff, // COMBOBOX_PRESSED_LIST_INSIDE_COLOR | |
| 0x0078acff, // COMBOBOX_PRESSED_LIST_TEXT_COLOR | |
| 0x828282ff, // CHECKBOX_DEFAULT_BORDER_COLOR | |
| 0xeeeeeeff, // CHECKBOX_DEFAULT_INSIDE_COLOR | |
| 0xc8c8c8ff, // CHECKBOX_HOVER_BORDER_COLOR | |
| 0xffffffff, // CHECKBOX_HOVER_INSIDE_COLOR | |
| 0x66e7ffff, // CHECKBOX_CLICK_BORDER_COLOR | |
| 0x3f3f3fff, // CHECKBOX_CLICK_INSIDE_COLOR | |
| 0xbcecffff, // CHECKBOX_STATUS_ACTIVE_COLOR | |
| 5, // CHECKBOX_INSIDE_WIDTH | |
| 1, // TEXTBOX_BORDER_WIDTH | |
| 0x828282ff, // TEXTBOX_BORDER_COLOR | |
| 0xf5f5f5ff, // TEXTBOX_INSIDE_COLOR | |
| 0x000000ff, // TEXTBOX_TEXT_COLOR | |
| 0x000000ff, // TEXTBOX_LINE_COLOR | |
| 10, // TEXTBOX_TEXT_FONTSIZE | |
| } | |
| // GUI property names (to read/write style text files) | |
| var propertyName = []string{ | |
| "GLOBAL_BASE_COLOR", | |
| "GLOBAL_BORDER_COLOR", | |
| "GLOBAL_TEXT_COLOR", | |
| "GLOBAL_TEXT_FONTSIZE", | |
| "GLOBAL_BORDER_WIDTH", | |
| "BACKGROUND_COLOR", | |
| "LINES_COLOR", | |
| "LABEL_BORDER_WIDTH", | |
| "LABEL_TEXT_COLOR", | |
| "LABEL_TEXT_PADDING", | |
| "BUTTON_BORDER_WIDTH", | |
| "BUTTON_TEXT_PADDING", | |
| "BUTTON_DEFAULT_BORDER_COLOR", | |
| "BUTTON_DEFAULT_INSIDE_COLOR", | |
| "BUTTON_DEFAULT_TEXT_COLOR", | |
| "BUTTON_HOVER_BORDER_COLOR", | |
| "BUTTON_HOVER_INSIDE_COLOR", | |
| "BUTTON_HOVER_TEXT_COLOR", | |
| "BUTTON_PRESSED_BORDER_COLOR", | |
| "BUTTON_PRESSED_INSIDE_COLOR", | |
| "BUTTON_PRESSED_TEXT_COLOR", | |
| "TOGGLE_TEXT_PADDING", | |
| "TOGGLE_BORDER_WIDTH", | |
| "TOGGLE_DEFAULT_BORDER_COLOR", | |
| "TOGGLE_DEFAULT_INSIDE_COLOR", | |
| "TOGGLE_DEFAULT_TEXT_COLOR", | |
| "TOGGLE_HOVER_BORDER_COLOR", | |
| "TOGGLE_HOVER_INSIDE_COLOR", | |
| "TOGGLE_HOVER_TEXT_COLOR", | |
| "TOGGLE_PRESSED_BORDER_COLOR", | |
| "TOGGLE_PRESSED_INSIDE_COLOR", | |
| "TOGGLE_PRESSED_TEXT_COLOR", | |
| "TOGGLE_ACTIVE_BORDER_COLOR", | |
| "TOGGLE_ACTIVE_INSIDE_COLOR", | |
| "TOGGLE_ACTIVE_TEXT_COLOR", | |
| "TOGGLEGROUP_PADDING", | |
| "SLIDER_BORDER_WIDTH", | |
| "SLIDER_BUTTON_BORDER_WIDTH", | |
| "SLIDER_BORDER_COLOR", | |
| "SLIDER_INSIDE_COLOR", | |
| "SLIDER_DEFAULT_COLOR", | |
| "SLIDER_HOVER_COLOR", | |
| "SLIDER_ACTIVE_COLOR", | |
| "SLIDERBAR_BORDER_COLOR", | |
| "SLIDERBAR_INSIDE_COLOR", | |
| "SLIDERBAR_DEFAULT_COLOR", | |
| "SLIDERBAR_HOVER_COLOR", | |
| "SLIDERBAR_ACTIVE_COLOR", | |
| "SLIDERBAR_ZERO_LINE_COLOR", | |
| "PROGRESSBAR_BORDER_COLOR", | |
| "PROGRESSBAR_INSIDE_COLOR", | |
| "PROGRESSBAR_PROGRESS_COLOR", | |
| "PROGRESSBAR_BORDER_WIDTH", | |
| "SPINNER_LABEL_BORDER_COLOR", | |
| "SPINNER_LABEL_INSIDE_COLOR", | |
| "SPINNER_DEFAULT_BUTTON_BORDER_COLOR", | |
| "SPINNER_DEFAULT_BUTTON_INSIDE_COLOR", | |
| "SPINNER_DEFAULT_SYMBOL_COLOR", | |
| "SPINNER_DEFAULT_TEXT_COLOR", | |
| "SPINNER_HOVER_BUTTON_BORDER_COLOR", | |
| "SPINNER_HOVER_BUTTON_INSIDE_COLOR", | |
| "SPINNER_HOVER_SYMBOL_COLOR", | |
| "SPINNER_HOVER_TEXT_COLOR", | |
| "SPINNER_PRESSED_BUTTON_BORDER_COLOR", | |
| "SPINNER_PRESSED_BUTTON_INSIDE_COLOR", | |
| "SPINNER_PRESSED_SYMBOL_COLOR", | |
| "SPINNER_PRESSED_TEXT_COLOR", | |
| "COMBOBOX_PADDING", | |
| "COMBOBOX_BUTTON_WIDTH", | |
| "COMBOBOX_BUTTON_HEIGHT", | |
| "COMBOBOX_BORDER_WIDTH", | |
| "COMBOBOX_DEFAULT_BORDER_COLOR", | |
| "COMBOBOX_DEFAULT_INSIDE_COLOR", | |
| "COMBOBOX_DEFAULT_TEXT_COLOR", | |
| "COMBOBOX_DEFAULT_LIST_TEXT_COLOR", | |
| "COMBOBOX_HOVER_BORDER_COLOR", | |
| "COMBOBOX_HOVER_INSIDE_COLOR", | |
| "COMBOBOX_HOVER_TEXT_COLOR", | |
| "COMBOBOX_HOVER_LIST_TEXT_COLOR", | |
| "COMBOBOX_PRESSED_BORDER_COLOR", | |
| "COMBOBOX_PRESSED_INSIDE_COLOR", | |
| "COMBOBOX_PRESSED_TEXT_COLOR", | |
| "COMBOBOX_PRESSED_LIST_BORDER_COLOR", | |
| "COMBOBOX_PRESSED_LIST_INSIDE_COLOR", | |
| "COMBOBOX_PRESSED_LIST_TEXT_COLOR", | |
| "CHECKBOX_DEFAULT_BORDER_COLOR", | |
| "CHECKBOX_DEFAULT_INSIDE_COLOR", | |
| "CHECKBOX_HOVER_BORDER_COLOR", | |
| "CHECKBOX_HOVER_INSIDE_COLOR", | |
| "CHECKBOX_CLICK_BORDER_COLOR", | |
| "CHECKBOX_CLICK_INSIDE_COLOR", | |
| "CHECKBOX_STATUS_ACTIVE_COLOR", | |
| "CHECKBOX_INSIDE_WIDTH", | |
| "TEXTBOX_BORDER_WIDTH", | |
| "TEXTBOX_BORDER_COLOR", | |
| "TEXTBOX_INSIDE_COLOR", | |
| "TEXTBOX_TEXT_COLOR", | |
| "TEXTBOX_LINE_COLOR", | |
| "TEXTBOX_TEXT_FONTSIZE", | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "strconv" | |
| "github.com/faiface/pixel" | |
| "github.com/faiface/pixel/pixelgl" | |
| "github.com/faiface/pixel/text" | |
| "golang.org/x/image/font/basicfont" | |
| "github.com/faiface/pixel/imdraw" | |
| ) | |
| // uint32(style[ToggleActiveBorderColor]) | |
| func hexToPixelRGBA(hex uint32) (pixel.RGBA) { | |
| hexString := fmt.Sprintf("%x", hex) | |
| //fmt.Printf("%s\n", hexString) | |
| var r_f, g_f, b_f, a_f float64 | |
| var r, g, b, a uint64 | |
| r, _ = strconv.ParseUint("0x" + hexString[0:2], 0, 64) | |
| g, _ = strconv.ParseUint("0x" + hexString[2:4], 0, 64) | |
| b, _ = strconv.ParseUint("0x" + hexString[4:6], 0, 64) | |
| a, _ = strconv.ParseUint("0x" + hexString[6:8], 0, 64) | |
| r_f = float64(r) / 255 | |
| g_f = float64(g) / 255 | |
| b_f = float64(b) / 255 | |
| a_f = float64(a) / 255 | |
| rgba := pixel.RGBA { | |
| R: r_f, | |
| G: g_f, | |
| B: b_f, | |
| A: a_f, | |
| } | |
| return rgba | |
| } | |
| func hexToPixelRGB(hex uint32) (pixel.RGBA) { | |
| hexString := fmt.Sprintf("%x", hex) | |
| //fmt.Printf("%s\n", hexString) | |
| var r_f, g_f, b_f float64 | |
| var r, g, b uint64 | |
| r, _ = strconv.ParseUint("0x" + hexString[0:2], 0, 64) | |
| g, _ = strconv.ParseUint("0x" + hexString[2:4], 0, 64) | |
| b, _ = strconv.ParseUint("0x" + hexString[4:6], 0, 64) | |
| r_f = float64(r) / 255 | |
| g_f = float64(g) / 255 | |
| b_f = float64(b) / 255 | |
| return pixel.RGB(r_f, g_f, b_f) | |
| } | |
| type Button struct { | |
| text string | |
| x float64 | |
| y float64 | |
| w float64 | |
| h float64 | |
| pressed bool | |
| } | |
| type Tickbox struct { | |
| text string | |
| x float64 | |
| y float64 | |
| w float64 | |
| h float64 | |
| ticked bool | |
| } | |
| func (t *Tickbox) draw(imd *imdraw.IMDraw, win *pixelgl.Window, tick bool) { | |
| // button outline | |
| //imd.Color = pixel.RGB(0.6, 0.6, 0.6) | |
| imd.Color = hexToPixelRGB(uint32(style[CheckboxDefaultBorderColor])) | |
| inside_width := float64(style[CheckboxInsideWidth]) | |
| imd.Push(pixel.V(t.x+(t.h/inside_width), t.y+(t.h/inside_width)), pixel.V(t.x+t.h-(t.h/inside_width), t.y+t.h-(t.h/inside_width))) | |
| imd.Rectangle(t.h/float64(style[ComboboxBorderWidth])) | |
| if win.JustPressed(pixelgl.MouseButtonLeft) { | |
| mousePos := win.MousePosition() | |
| if mousePos.X <= t.x+t.h && mousePos.X >= t.x && mousePos.Y <= t.y+t.h && mousePos.Y >= t.y { | |
| if t.ticked == false { | |
| t.ticked = true | |
| fmt.Println("box ticked!") | |
| } else if t.ticked == true { | |
| t.ticked = false | |
| fmt.Println("box unticked!") | |
| } | |
| } | |
| } | |
| //imd.Color = pixel.RGB(0.85, 0.85, 0.85) | |
| imd.Color = hexToPixelRGB(uint32(style[CheckboxDefaultInsideColor])) | |
| imd.Push(pixel.V(t.x+(t.h/inside_width), t.y+(t.h/inside_width)), pixel.V(t.x+t.h-(t.h/inside_width), t.y+t.h-(t.h/inside_width))) | |
| imd.Rectangle(0) | |
| if t.ticked == true && tick == true { | |
| //imd.Color = pixel.RGB(0.4, 0.4, 0.4) | |
| imd.Color = hexToPixelRGB(uint32(style[CheckboxClickInsideColor])) | |
| imd.Push(pixel.V(t.x+(t.h/4), t.y+(t.h/4)), pixel.V(t.x+t.h-(t.h/4), t.y+t.h-(t.h/4))) | |
| imd.Line(t.h/inside_width) | |
| imd.Push(pixel.V(t.x+t.h-(t.h/4), t.y+(t.h/4)), pixel.V(t.x+(t.h/4), t.y+t.h-(t.h/4))) | |
| imd.Line(t.h/inside_width) | |
| } else if t.ticked == true && tick == false { | |
| imd.Color = hexToPixelRGB(uint32(style[ComboboxPressedInsideColor])) | |
| imd.Push(pixel.V(t.x+(t.h/float64(style[boundsWidth])), t.y+(t.h/float64(style[boundsWidth]))), pixel.V(t.x+t.h-(t.h/float64(style[boundsWidth])), t.y+t.h-(t.h/float64(style[boundsWidth])))) | |
| imd.Rectangle(0) | |
| } else if t.ticked == false { | |
| //imd.Color = pixel.RGB(0.85, 0.85, 0.85) | |
| } | |
| imd.Draw(win) | |
| } | |
| func (t *Tickbox) drawText(imd *imdraw.IMDraw, win *pixelgl.Window) { | |
| // button text | |
| atlas := text.NewAtlas(basicfont.Face7x13, text.ASCII) | |
| basicTxt := text.New(pixel.V(t.x+t.h*1.5, t.y+t.h/4), atlas) | |
| basicTxt.Color = hexToPixelRGB(uint32(style[GlobalTextColor])) | |
| //fmt.Println(basicTxt.Bounds()) | |
| //basicTxt.Bounds().Vertices[3] - basicTxt.Bounds().Vertices[1] | |
| scale := 2.0 | |
| scale = t.h / 20.0 | |
| basicTxt.Draw(win, pixel.IM.Scaled(basicTxt.Orig, scale)) | |
| } | |
| func (b *Button) draw(imd *imdraw.IMDraw, win *pixelgl.Window) { | |
| // button outline | |
| imd.Color = hexToPixelRGB(uint32(style[ButtonDefaultBorderColor])) | |
| //imd.Color = pixel.RGB(0.6, 0.6, 0.6) | |
| imd.Push(pixel.V(b.x, b.y), pixel.V(b.x+b.w, b.y+b.h)) | |
| imd.Rectangle(b.h/float64(style[ButtonBorderWidth])) | |
| if win.Pressed(pixelgl.MouseButtonLeft) { | |
| mousePos := win.MousePosition() | |
| if mousePos.X <= b.x+b.w && mousePos.X >= b.x && mousePos.Y <= b.y+b.h && mousePos.Y >= b.y { | |
| fmt.Println("button pressed!") | |
| b.pressed = true | |
| imd.Color = hexToPixelRGB(uint32(style[ButtonPressedInsideColor])) | |
| } else { | |
| b.pressed = false | |
| imd.Color = hexToPixelRGB(uint32(style[ButtonDefaultInsideColor])) | |
| } | |
| } else { | |
| imd.Color = hexToPixelRGB(uint32(style[ButtonDefaultInsideColor])) | |
| b.pressed = false | |
| } | |
| imd.Push(pixel.V(b.x, b.y), pixel.V(b.x+b.w, b.y+b.h)) | |
| imd.Rectangle(0) | |
| imd.Draw(win) | |
| } | |
| func (b *Button) drawText(imd *imdraw.IMDraw, win *pixelgl.Window) { | |
| // button text | |
| atlas := text.NewAtlas(basicfont.Face7x13, text.ASCII) | |
| basicTxt := text.New(pixel.V(b.x+b.w/float64(style[ButtonTextPadding]), b.y+b.h/float64(style[ButtonTextPadding])), atlas) | |
| basicTxt.Color = hexToPixelRGB(uint32(style[ButtonDefaultTextColor])) | |
| fmt.Fprintln(basicTxt, b.text) | |
| //fmt.Println(basicTxt.Bounds()) | |
| //basicTxt.Bounds().Vertices[3] - basicTxt.Bounds().Vertices[1] | |
| scale := 2.0 | |
| scale = b.h / 20.0 | |
| basicTxt.Draw(win, pixel.IM.Scaled(basicTxt.Orig, scale)) | |
| } | |
| func run() { | |
| cfg := pixelgl.WindowConfig{ | |
| Title: "TEST!", | |
| Bounds: pixel.R(0, 0, 800, 600), | |
| VSync: true, | |
| } | |
| win, err := pixelgl.NewWindow(cfg) | |
| if err != nil { | |
| panic(err) | |
| } | |
| imd := imdraw.New(nil) | |
| win.SetSmooth(true) | |
| goButton := Button { | |
| text: "Go!", | |
| x: 200, | |
| y: 60, | |
| w: 80, | |
| h: 40, | |
| pressed: false, | |
| } | |
| stopButton := Button { | |
| text: "STOP!", | |
| x: 400, | |
| y: 450, | |
| w: 300, | |
| h: 100, | |
| pressed: false, | |
| } | |
| fastTick := Tickbox { | |
| text: "Fast?", | |
| x: 400, | |
| y: 100, | |
| w: 80, | |
| h: 50, | |
| ticked: false, | |
| } | |
| bigOpt := Tickbox { | |
| text: "Big", | |
| x: 300, | |
| y: 150, | |
| w: 80, | |
| h: 50, | |
| ticked: false, | |
| } | |
| smallOpt := Tickbox { | |
| text: "Small", | |
| x: 350, | |
| y: 200, | |
| w: 80, | |
| h: 50, | |
| ticked: false, | |
| } | |
| for !win.Closed() { | |
| win.Clear(hexToPixelRGB(uint32(style[GlobalBackgroundColor]))) | |
| goButton.draw(imd, win) | |
| stopButton.draw(imd, win) | |
| fastTick.draw(imd, win, true) | |
| bigOpt.draw(imd, win, false) | |
| smallOpt.draw(imd, win, false) | |
| goButton.drawText(imd, win) | |
| stopButton.drawText(imd, win) | |
| fastTick.drawText(imd, win) | |
| bigOpt.drawText(imd, win) | |
| smallOpt.drawText(imd, win) | |
| win.Update() | |
| } | |
| } | |
| func main() { | |
| pixelgl.Run(run) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment