ui.gadgets.tables: support icons
parent
cfa285a646
commit
66392108e7
|
@ -8,8 +8,9 @@ IN: ui.gadgets.line-support
|
||||||
! Some code shared by table and editor gadgets
|
! Some code shared by table and editor gadgets
|
||||||
SLOT: font
|
SLOT: font
|
||||||
|
|
||||||
: line-height ( gadget -- n )
|
GENERIC: line-height ( gadget -- n )
|
||||||
font>> "" text-height ;
|
|
||||||
|
M: gadget line-height font>> "" text-height ;
|
||||||
|
|
||||||
: y>line ( y gadget -- n )
|
: y>line ( y gadget -- n )
|
||||||
line-height /i ;
|
line-height /i ;
|
||||||
|
|
|
@ -4,11 +4,12 @@ USING: accessors arrays colors colors.constants fry kernel math
|
||||||
math.rectangles math.order math.vectors namespaces opengl sequences
|
math.rectangles math.order math.vectors namespaces opengl sequences
|
||||||
ui.gadgets ui.gadgets.scrollers ui.gadgets.status-bar
|
ui.gadgets ui.gadgets.scrollers ui.gadgets.status-bar
|
||||||
ui.gadgets.worlds ui.gadgets.theme ui.gestures ui.render ui.text
|
ui.gadgets.worlds ui.gadgets.theme ui.gestures ui.render ui.text
|
||||||
ui.gadgets.menus ui.gadgets.line-support math.rectangles models
|
ui.images ui.gadgets.menus ui.gadgets.line-support math.rectangles
|
||||||
math.ranges sequences combinators fonts locals ;
|
models math.ranges sequences combinators fonts locals strings ;
|
||||||
IN: ui.gadgets.tables
|
IN: ui.gadgets.tables
|
||||||
|
|
||||||
! Row rendererer protocol
|
! Row rendererer protocol
|
||||||
|
GENERIC: prototype-row ( renderer -- columns )
|
||||||
GENERIC: row-columns ( row renderer -- columns )
|
GENERIC: row-columns ( row renderer -- columns )
|
||||||
GENERIC: row-value ( row renderer -- object )
|
GENERIC: row-value ( row renderer -- object )
|
||||||
GENERIC: row-color ( row renderer -- color )
|
GENERIC: row-color ( row renderer -- color )
|
||||||
|
@ -16,14 +17,22 @@ GENERIC: row-color ( row renderer -- color )
|
||||||
SINGLETON: trivial-renderer
|
SINGLETON: trivial-renderer
|
||||||
|
|
||||||
M: trivial-renderer row-columns drop ;
|
M: trivial-renderer row-columns drop ;
|
||||||
|
M: object prototype-row drop { "" } ;
|
||||||
M: object row-value drop ;
|
M: object row-value drop ;
|
||||||
M: object row-color 2drop f ;
|
M: object row-color 2drop f ;
|
||||||
|
|
||||||
TUPLE: table < gadget
|
TUPLE: table < gadget
|
||||||
renderer filled-column column-alignment action single-click? hook
|
{ renderer initial: trivial-renderer }
|
||||||
|
filled-column column-alignment
|
||||||
|
{ action initial: [ drop ] }
|
||||||
|
single-click?
|
||||||
|
{ hook initial: [ ] }
|
||||||
|
{ gap initial: 6 }
|
||||||
column-widths total-width
|
column-widths total-width
|
||||||
font selection-color focus-border-color
|
font selection-color focus-border-color
|
||||||
mouse-color column-line-color selection-required?
|
{ mouse-color initial: COLOR: black }
|
||||||
|
{ column-line-color initial: COLOR: dark-gray }
|
||||||
|
selection-required?
|
||||||
selected-index selected-value
|
selected-index selected-value
|
||||||
mouse-index
|
mouse-index
|
||||||
focused? ;
|
focused? ;
|
||||||
|
@ -31,32 +40,46 @@ focused? ;
|
||||||
: <table> ( rows -- table )
|
: <table> ( rows -- table )
|
||||||
table new-gadget
|
table new-gadget
|
||||||
swap >>model
|
swap >>model
|
||||||
trivial-renderer >>renderer
|
|
||||||
[ drop ] >>action
|
|
||||||
[ ] >>hook
|
|
||||||
f <model> >>selected-value
|
f <model> >>selected-value
|
||||||
sans-serif-font >>font
|
sans-serif-font >>font
|
||||||
selection-color >>selection-color
|
selection-color >>selection-color
|
||||||
focus-border-color >>focus-border-color
|
focus-border-color >>focus-border-color ;
|
||||||
COLOR: dark-gray >>column-line-color
|
|
||||||
COLOR: black >>mouse-color ;
|
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
CONSTANT: table-gap 6
|
GENERIC: cell-width ( font cell -- x )
|
||||||
|
GENERIC: cell-height ( font cell -- y )
|
||||||
|
GENERIC: draw-cell ( font cell -- )
|
||||||
|
|
||||||
|
M: string cell-width text-width ;
|
||||||
|
M: string cell-height text-height ;
|
||||||
|
M: string draw-cell draw-text ;
|
||||||
|
|
||||||
|
M: image-name cell-width nip image-dim first ;
|
||||||
|
M: image-name cell-height nip image-dim second ;
|
||||||
|
M: image-name draw-cell nip draw-image ;
|
||||||
|
|
||||||
: table-rows ( table -- rows )
|
: table-rows ( table -- rows )
|
||||||
[ control-value ] [ renderer>> ] bi '[ _ row-columns ] map ;
|
[ control-value ] [ renderer>> ] bi '[ _ row-columns ] map ;
|
||||||
|
|
||||||
: (compute-column-widths) ( font rows -- total widths )
|
: column-offsets ( widths gap -- x xs )
|
||||||
[ drop 0 { } ] [
|
[ 0 ] dip '[ _ + + ] accumulate ;
|
||||||
[ nip first length 0 <repetition> ] 2keep
|
|
||||||
[ [ text-width ] with map vmax ] with each
|
: initial-widths ( rows -- widths )
|
||||||
[ [ sum ] [ length 1 [-] table-gap * ] bi + ] keep
|
first length 0 <repetition> ;
|
||||||
|
|
||||||
|
: row-column-widths ( font row -- widths )
|
||||||
|
[ cell-width ] with map ;
|
||||||
|
|
||||||
|
: (compute-column-widths) ( gap font rows -- total widths )
|
||||||
|
[ 2drop 0 { } ] [
|
||||||
|
[ nip initial-widths ] 2keep
|
||||||
|
[ row-column-widths vmax ] with each
|
||||||
|
[ swap [ column-offsets drop ] keep - ] keep
|
||||||
] if-empty ;
|
] if-empty ;
|
||||||
|
|
||||||
: compute-column-widths ( table -- total-width column-widths )
|
: compute-column-widths ( table -- total-width column-widths )
|
||||||
[ font>> ] [ table-rows ] bi (compute-column-widths) ;
|
[ gap>> ] [ font>> ] [ table-rows ] tri (compute-column-widths) ;
|
||||||
|
|
||||||
: update-cached-widths ( table -- )
|
: update-cached-widths ( table -- )
|
||||||
dup compute-column-widths
|
dup compute-column-widths
|
||||||
|
@ -108,28 +131,32 @@ M: table layout*
|
||||||
over mouse-color>> [ gl-rect ] highlight-row
|
over mouse-color>> [ gl-rect ] highlight-row
|
||||||
] [ 2drop ] if ;
|
] [ 2drop ] if ;
|
||||||
|
|
||||||
: column-offsets ( table -- xs )
|
: column-line-offsets ( widths gap -- xs )
|
||||||
0 [ table-gap + + ] accumulate nip ;
|
[ column-offsets nip [ f ] ]
|
||||||
|
[ 2/ '[ rest-slice [ _ - ] map ] ]
|
||||||
|
bi if-empty ;
|
||||||
|
|
||||||
: column-line-offsets ( table -- xs )
|
: draw-column-lines ( table -- )
|
||||||
column-offsets
|
|
||||||
[ f ] [ rest-slice [ table-gap 2/ - ] map ] if-empty ;
|
|
||||||
|
|
||||||
: draw-columns ( table -- )
|
|
||||||
[ column-line-color>> gl-color ]
|
[ column-line-color>> gl-color ]
|
||||||
[
|
[
|
||||||
[ column-widths>> column-line-offsets ] [ dim>> second ] bi
|
[ [ column-widths>> ] [ gap>> ] bi column-line-offsets ] [ dim>> second ] bi
|
||||||
'[ [ 0 2array ] [ _ 2array ] bi gl-line ] each
|
'[ [ 0 2array ] [ _ 2array ] bi gl-line ] each
|
||||||
] bi ;
|
] bi ;
|
||||||
|
|
||||||
: column-loc ( font column width align -- loc )
|
: column-loc ( font column width align -- loc )
|
||||||
[ [ text-width ] dip swap - ] dip
|
[ [ cell-width ] dip swap - ] dip
|
||||||
* 0 2array ;
|
* 0 2array ;
|
||||||
|
|
||||||
: draw-column ( font column width align -- )
|
: translate-column ( width gap -- )
|
||||||
over [
|
+ 0 2array gl-translate ;
|
||||||
[ 2dup ] 2dip column-loc [ draw-text ] with-translation
|
|
||||||
] dip table-gap + 0 2array gl-translate ;
|
: draw-column ( font column width align gap -- )
|
||||||
|
[
|
||||||
|
over [
|
||||||
|
[ 2dup ] 2dip column-loc
|
||||||
|
[ draw-cell ] with-translation
|
||||||
|
] dip
|
||||||
|
] dip translate-column ;
|
||||||
|
|
||||||
: column-alignment ( table -- seq )
|
: column-alignment ( table -- seq )
|
||||||
dup column-alignment>>
|
dup column-alignment>>
|
||||||
|
@ -147,25 +174,29 @@ M: table draw-line ( row index table -- )
|
||||||
[ column-widths>> ]
|
[ column-widths>> ]
|
||||||
[ column-alignment ]
|
[ column-alignment ]
|
||||||
tri
|
tri
|
||||||
] [ row-font ] 3bi
|
]
|
||||||
'[ [ _ ] 3dip draw-column ] 3each ;
|
[ row-font ]
|
||||||
|
[ 2nip gap>> ] 3tri
|
||||||
|
'[ [ _ ] 3dip _ draw-column ] 3each ;
|
||||||
|
|
||||||
M: table draw-gadget*
|
M: table draw-gadget*
|
||||||
dup control-value empty? [ drop ] [
|
dup control-value empty? [ drop ] [
|
||||||
{
|
{
|
||||||
[ draw-selected-row ]
|
[ draw-selected-row ]
|
||||||
[ draw-columns ]
|
|
||||||
[ draw-lines ]
|
[ draw-lines ]
|
||||||
|
[ draw-column-lines ]
|
||||||
[ draw-focused-row ]
|
[ draw-focused-row ]
|
||||||
[ draw-moused-row ]
|
[ draw-moused-row ]
|
||||||
} cleave
|
} cleave
|
||||||
] if ;
|
] if ;
|
||||||
|
|
||||||
|
M: table line-height ( table -- y )
|
||||||
|
[ font>> ] [ renderer>> prototype-row ] bi
|
||||||
|
[ cell-height ] with [ max ] map-reduce ;
|
||||||
|
|
||||||
M: table pref-dim*
|
M: table pref-dim*
|
||||||
[ compute-column-widths drop ] keep
|
[ compute-column-widths drop ] keep
|
||||||
[ font>> "" text-height ]
|
[ line-height ] [ control-value length ] bi * 2array ;
|
||||||
[ control-value length ]
|
|
||||||
bi * 2array ;
|
|
||||||
|
|
||||||
: nth-row ( row table -- value/f ? )
|
: nth-row ( row table -- value/f ? )
|
||||||
over [ control-value nth t ] [ 2drop f f ] if ;
|
over [ control-value nth t ] [ 2drop f f ] if ;
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Slava Pestov
|
|
@ -0,0 +1,4 @@
|
||||||
|
! Copyright (C) 2009 Slava Pestov.
|
||||||
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
|
USING: tools.test ui.images ;
|
||||||
|
IN: ui.images.tests
|
|
@ -0,0 +1,45 @@
|
||||||
|
! Copyright (C) 2009 Slava Pestov.
|
||||||
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
|
USING: namespaces cache images accessors assocs kernel
|
||||||
|
opengl.gl opengl.texture-cache ui.gadgets.worlds ;
|
||||||
|
IN: ui.images
|
||||||
|
|
||||||
|
TUPLE: image-name path ;
|
||||||
|
|
||||||
|
C: <image-name> image-name
|
||||||
|
|
||||||
|
<PRIVATE
|
||||||
|
|
||||||
|
SYMBOL: image-cache
|
||||||
|
|
||||||
|
image-cache [ <cache-assoc> ] initialize
|
||||||
|
|
||||||
|
PRIVATE>
|
||||||
|
|
||||||
|
: cached-image ( image-name -- image )
|
||||||
|
path>> image-cache get [ <image> ] cache ;
|
||||||
|
|
||||||
|
<PRIVATE
|
||||||
|
|
||||||
|
SINGLETON: image-renderer
|
||||||
|
|
||||||
|
M: image-renderer render-texture
|
||||||
|
drop
|
||||||
|
cached-image
|
||||||
|
[ dim>> ] [ bitmap>> ] bi
|
||||||
|
GL_RGBA GL_UNSIGNED_BYTE <texture-info> ;
|
||||||
|
|
||||||
|
: image-texture-cache ( world -- texture-cache )
|
||||||
|
[ [ image-renderer <texture-cache> ] unless* ] change-images
|
||||||
|
images>> ;
|
||||||
|
|
||||||
|
PRIVATE>
|
||||||
|
|
||||||
|
: rendered-image ( path -- texture )
|
||||||
|
world get image-texture-cache get-texture ;
|
||||||
|
|
||||||
|
: draw-image ( image-name -- )
|
||||||
|
rendered-image display-list>> glCallList ;
|
||||||
|
|
||||||
|
: image-dim ( image-name -- dim )
|
||||||
|
cached-image dim>> ;
|
Loading…
Reference in New Issue