Skip to content

Commit

Permalink
Imported table gallery example from andlabs/libui
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanyuan Li authored and Hanyuan Li committed Aug 26, 2018
1 parent e8ccb35 commit e445e9f
Show file tree
Hide file tree
Showing 18 changed files with 470 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,2 +1,3 @@
.DS_Store
/bin
/bin
/lib
8 changes: 4 additions & 4 deletions examples/control_gallery.cr
Expand Up @@ -57,23 +57,23 @@ class ControlGallery < Hedron::Application
open = Hedron::MenuItem.new(file_menu, "Open")
save = Hedron::MenuItem.new(file_menu, "Save")

file_menu.add_quit
file_menu.push(:quit)
self.on_stop = ->should_quit

edit_menu = Hedron::Menu.new("Edit")
checkable = Hedron::MenuCheckItem.new(edit_menu, "Checkable item")

edit_menu.add_separator
edit_menu.push(:separator)

disabled = Hedron::MenuItem.new(edit_menu, "Disabled Item")
disabled.disable

edit_menu.add_preferences
edit_menu.push(:preferences)

help_menu = Hedron::Menu.new("Help")
help = Hedron::MenuItem.new(help_menu, "Help")

help_menu.add_about
help_menu.push(:about)

@mainwin = Hedron::Window.new("Hedron Control Gallery", {640, 480}, menubar: true)
mainwin = @mainwin.not_nil!
Expand Down
5 changes: 0 additions & 5 deletions examples/ml_gallery.cr

This file was deleted.

Expand Up @@ -34,3 +34,7 @@ class MLGallery < Hedron::Application
@window.not_nil!.show
end
end

gallery = MLGallery.new
gallery.start
gallery.close
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
137 changes: 137 additions & 0 deletions examples/table_gallery/main.cr
@@ -0,0 +1,137 @@
require "../../src/hedron.cr"

class TableGallery < Hedron::Application
@images = [] of Hedron::Image
@row_9_text = ""
@yellow_row = -1
@states = [] of Int32

def on_closing(this)
this.destroy
self.stop
return false
end

def should_quit
@window.not_nil!.destroy
return true
end

def draw
self.on_stop = ->should_quit

@window = Hedron::Window.new("Table Gallery", {640, 480}, menubar: true)
@window.not_nil!.on_close = ->on_closing(Hedron::Window)

@images.push(Hedron::Image.new(16, 16))
@images[0].push_png("./images/andlabs_16x16test_24june2016.png")
@images[0].push_png("./images/andlabs_32x32test_24june2016.png")

@images.push(Hedron::Image.new(16, 16))
@images[1].push_png("./images/tango-icon-theme-0.8.90_16x16_x-office-spreadsheet.png")
@images[1].push_png("./images/tango-icon-theme-0.8.90_32x32_x-office-spreadsheet.png")

@row_9_text = "Part"

@states = Array(Int32).new(15) { 0 }

page = Hedron::VerticalBox.new
@model = Hedron::Table::Model.new(self)

params = Hedron::Table::Params.new(@model.not_nil!, 3)

table = Hedron::Table.new(params)
table.stretchy = true
page.push(table)

table.push_text_column("Column 1", Hedron::Table::TextColumn.new(0, -1))
text_params = Hedron::Table::TextColumnParams.new(4)
table.push_image_text_column(
"Column 2",
Hedron::Table::ImageColumn.new(5),
Hedron::Table::TextColumn.new(1, -1, text_params)
)
table.push_text_column("Editable", Hedron::Table::TextColumn.new(2, -2))

table.push_checkbox_column("Checkboxes", Hedron::Table::CheckboxColumn.new(7, -2))
table.push_button_column("Buttons", Hedron::Table::ButtonColumn.new(8, -2))
table.push_progress_bar_column("Progress Bar", Hedron::Table::ProgressBarColumn.new(6))

@window.not_nil!.child = page
@window.not_nil!.show
end
end

class TableGallery
include Hedron::Table::ModelHandler

def columns
return 9
end

def rows
return 15
end

def column_type(column)
case column
when 3, 4 then UI::TableValueType::Color
when 5 then UI::TableValueType::Image
when 7, 8 then UI::TableValueType::Int
else UI::TableValueType::String
end
end

def [](row, column)
return case column
when 0
Hedron::Table::Value.new("Row #{row}")
when 1
Hedron::Table::Value.new("Part")
when 2
Hedron::Table::Value.new(row == 9 ? @row_9_text : "Part")
when 3
case row
when @yellow_row
Hedron::Table::Value.new(Hedron::Color.new(255_u8, 255_u8, 0_u8, 255_u8))
when 3
Hedron::Table::Value.new(Hedron::Color.new(255_u8, 0_u8, 0_u8, 255_u8))
when 11
Hedron::Table::Value.new(Hedron::Color.new(0_u8, 127_u8, 255_u8, 0_u8))
end
when 4
row.odd? ? Hedron::Table::Value.new(Hedron::Color.new(127_u8, 0_u8, 191_u8, 255_u8)) : nil
when 5
Hedron::Table::Value.new(row < 8 ? @images[0] : @images[1])
when 6
Hedron::Table::Value.new("Make Yellow")
when 7
Hedron::Table::Value.new(@states[row])
when 8
Hedron::Table::Value.new(case row
when 0 then 0
when 13 then 100
when 14 then -1
else 50
end)
end
end

def []=(row, column, value)
case column
when 2
@row_9_text = value.string if row == 9
when 6
prev_yellow_row = @yellow_row
@yellow_row = row

@model.not_nil!.row_changed(prev_yellow_row != -1 ? prev_yellow_row : @yellow_row)
when 7
@states[row] = value.int
end
end
end

gallery = TableGallery.new
gallery.start
gallery.close
10 changes: 10 additions & 0 deletions shard.lock
@@ -0,0 +1,10 @@
version: 1.0
shards:
stumpy_core:
github: stumpycr/stumpy_core
version: 1.8.2

stumpy_png:
github: stumpycr/stumpy_png
version: 4.4.1

7 changes: 6 additions & 1 deletion shard.yml
Expand Up @@ -7,6 +7,11 @@ authors:
description: |
A simple OOP UI library, with custom markup integration.
crystal: 0.24.1
dependencies:
stumpy_png:
github: stumpycr/stumpy_png
version: "~>4.4.1"

crystal: 0.26.0

license: MIT
12 changes: 6 additions & 6 deletions src/hedron/bindings.cr
Expand Up @@ -38,7 +38,7 @@ lib UI
Features
end

enum DrawBushType : LibC::UInt
enum DrawBrushType : LibC::UInt
Solid
LinearGradient
RadialGradient
Expand Down Expand Up @@ -457,13 +457,13 @@ lib UI
fun tab_num_pages = uiTabNumPages(t : Tab*) : LibC::Int
fun tab_set_margined = uiTabSetMargined(t : Tab*, page : LibC::Int, margined : LibC::Int)

fun table_append_text_column = uiTableAppendTextColumn(t : Table*, name : LibC::Char*, text_model_column : LibC::Int, text_editable_model_column : LibC::Int, text_params : TableTextColumnOptionalParams*)
fun table_append_image_column = uiTableAppendImageColumn(t : Table*, name : LibC::Char*, image_model_column : LibC::Int)
fun table_append_image_text_column = uiTableAppendImageTextColumn(t : Table*, name : LibC::Char*, image_model_column : LibC::Int, text_model_column : LibC::Int, text_editable_model_column : LibC::Int, text_params : TableTextColumnOptionalParams*)
fun table_append_button_column = uiTableAppendButtonColumn(t : Table*, name : LibC::Char*, button_model_column : LibC::Int, button_clickable_model_column : LibC::Int)
fun table_append_checkbox_column = uiTableAppendCheckboxColumn(t : Table*, name : LibC::Char*, checkbox_model_column : LibC::Int, checkbox_editable_model_column : LibC::Int)
fun table_append_checkbox_text_column = uiTableAppendCheckboxTextColumn(t : Table*, name : LibC::Char*, checkbox_model_column : LibC::Int, checkbox_editable_model_column : LibC::Int, text_model_column : LibC::Int, text_editable_model_column : LibC::Int, text_params : TableTextColumnOptionalParams*)
fun table_append_image_column = uiTableAppendImageColumn(t : Table*, name : LibC::Char*, image_model_column : LibC::Int)
fun table_append_image_text_column = uiTableAppendImageTextColumn(t : Table*, name : LibC::Char*, image_model_column : LibC::Int, text_model_column : LibC::Int, text_editable_model_column : LibC::Int, text_params : TableTextColumnOptionalParams*)
fun table_append_progress_bar_column = uiTableAppendProgressBarColumn(t : Table*, name : LibC::Char*, progress_model_column : LibC::Int)
fun table_append_button_column = uiTableAppendButtonColumn(t : Table*, name : LibC::Char*, button_model_column : LibC::Int, button_clickable_model_column : LibC::Int)
fun table_append_text_column = uiTableAppendTextColumn(t : Table*, name : LibC::Char*, text_model_column : LibC::Int, text_editable_model_column : LibC::Int, text_params : TableTextColumnOptionalParams*)

fun table_model_row_changed = uiTableModelRowChanged(m : TableModel*, index : LibC::Int)
fun table_model_row_deleted = uiTableModelRowDeleted(m : TableModel*, old_index : LibC::Int)
Expand Down Expand Up @@ -614,7 +614,7 @@ lib UI
column_type : (TableModelHandler*, TableModel*, LibC::Int -> TableValueType)
num_rows : (TableModelHandler*, TableModel* -> LibC::Int)
cell_value : (TableModelHandler*, TableModel*, LibC::Int, LibC::Int -> TableValue*)
set_cell_value : (TableModelHandler*, TableModel*, LibC::Int, LibC::Int, TableValue*)
set_cell_value : (TableModelHandler*, TableModel*, LibC::Int, LibC::Int, TableValue* -> Void)
end

struct TableParams
Expand Down
30 changes: 23 additions & 7 deletions src/hedron/menu/menu.cr
@@ -1,27 +1,43 @@
require "../bindings.cr"

module Hedron
enum MenuType
About
Preferences
Quit
Separator
end

class Menu
@this : UI::Menu*

def initialize(name : String)
@this = UI.new_menu(name)
end

def add_separator
UI.menu_append_separator(to_unsafe)
private def push_about
UI.menu_append_about_item(to_unsafe)
end

private def push_preferences
UI.menu_append_preferences_item(to_unsafe)
end

def add_quit
private def push_quit
UI.menu_append_quit_item(to_unsafe)
end

def add_preferences
UI.menu_append_preferences_item(to_unsafe)
private def push_separator
UI.menu_append_separator(to_unsafe)
end

def add_about
UI.menu_append_about_item(to_unsafe)
def push(menu_type : MenuType)
case menu_type
when MenuType::About then push_about
when MenuType::Preferences then push_preferences
when MenuType::Quit then push_quit
when MenuType::Separator then push_separator
end
end

def to_unsafe
Expand Down
4 changes: 4 additions & 0 deletions src/hedron/struct/color.cr
Expand Up @@ -7,5 +7,9 @@ module Hedron
getter alpha : UInt8

def initialize(@red, @green, @blue, @alpha); end

def to_u32 : UInt32
return (@red.to_u32 >> 24) + (@green.to_u32 >> 16) + (@blue.to_u32 >> 8) + @alpha.to_u32
end
end
end
14 changes: 7 additions & 7 deletions src/hedron/ui/grid.cr
Expand Up @@ -7,10 +7,10 @@ module Hedron
struct GridCell
property size : Tuple(Int32, Int32)
property expand : Tuple(Bool, Bool)
property align_x : Align
property align_y : Align
property align_x : UI::Align
property align_y : UI::Align

def initialize(@size, @expand, @align_x : Align, @align_y : Align); end
def initialize(@size, @expand, @align_x : UI::Align, @align_y : UI::Align); end
end

class Grid < Control
Expand All @@ -27,8 +27,8 @@ module Hedron
ui_control(next_to.control.to_unsafe),
side,
cell_info.size[0], cell_info.size[1],
to_int(cell_info.expand[0]), cell_info.align_x.value,
to_int(cell_info.expand[1]), cell_info.align_y.value
to_int(cell_info.expand[0]), cell_info.align_x,
to_int(cell_info.expand[1]), cell_info.align_y
)
end

Expand All @@ -46,8 +46,8 @@ module Hedron
ui_control(widget.control.to_unsafe),
coords[0], coords[1],
cell_info.size[0], cell_info.size[1],
to_int(cell_info.expand[0]), cell_info.align_x.value,
to_int(cell_info.expand[1]), cell_info.align_y.value
to_int(cell_info.expand[0]), cell_info.align_x,
to_int(cell_info.expand[1]), cell_info.align_y
)
end

Expand Down
32 changes: 32 additions & 0 deletions src/hedron/ui/image.cr
@@ -1,6 +1,38 @@
require "stumpy_png"
require "../bindings.cr"

module Hedron
class Image
@this : UI::Image*

def initialize(width : Int32, height : Int32)
@this = UI.new_image(width, height)
end

def initialize(@this); end

def push(pixels : Array(UInt32), width : Int32, height : Int32, byte_stride : Int32)
UI.image_append(to_unsafe, pixels.to_unsafe, width, height, byte_stride)
end

def push_png(filename : String)
puts filename
png = StumpyPNG.read(filename)

pixels = png.pixels.map do |pixel|
rgba = pixel.to_rgba
Color.new(rgba[0], rgba[1], rgba[2], rgba[3]).to_u32
end.to_a

width = png.width
height = png.height
stride = width * 4

push(pixels, width, height, stride)
end

def to_unsafe
return @this
end
end
end

0 comments on commit e445e9f

Please sign in to comment.