From 1e26d4256a4a91fac9493942739f8c9f975b4b59 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Thu, 12 Feb 2009 01:39:03 -0600 Subject: [PATCH] Add draw-scaled-image word, add some pens for rendering pixmaps --- basis/ui/gadgets/icons/icons.factor | 9 ++-- basis/ui/images/images.factor | 5 +- basis/ui/render/render.factor | 80 ++++++++++++++++++++++++----- 3 files changed, 75 insertions(+), 19 deletions(-) diff --git a/basis/ui/gadgets/icons/icons.factor b/basis/ui/gadgets/icons/icons.factor index 8f9bacad37..deba794aed 100644 --- a/basis/ui/gadgets/icons/icons.factor +++ b/basis/ui/gadgets/icons/icons.factor @@ -3,10 +3,9 @@ USING: kernel accessors ui.images ui.render ui.gadgets ; IN: ui.gadgets.icons -TUPLE: icon < gadget image ; +TUPLE: icon < gadget ; -: ( image-name -- icon ) icon new swap >>image ; +: ( image-name -- icon ) + icon new swap >>interior ; -M: icon draw-gadget* image>> draw-image ; - -M: icon pref-dim* image>> image-dim ; \ No newline at end of file +M: icon pref-dim* dup interior>> pen-pref-dim ; \ No newline at end of file diff --git a/basis/ui/images/images.factor b/basis/ui/images/images.factor index 18f9daab86..556a489703 100644 --- a/basis/ui/images/images.factor +++ b/basis/ui/images/images.factor @@ -1,7 +1,7 @@ ! 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 ; +opengl opengl.gl opengl.texture-cache ui.gadgets.worlds ; IN: ui.images TUPLE: image-name path ; @@ -41,5 +41,8 @@ PRIVATE> : draw-image ( image-name -- ) rendered-image display-list>> glCallList ; +: draw-scaled-image ( dim image-name -- ) + rendered-image texture>> draw-textured-rect ; + : image-dim ( image-name -- dim ) cached-image dim>> ; \ No newline at end of file diff --git a/basis/ui/render/render.factor b/basis/ui/render/render.factor index 9e3e0cae57..5712765949 100755 --- a/basis/ui/render/render.factor +++ b/basis/ui/render/render.factor @@ -1,9 +1,10 @@ ! Copyright (C) 2005, 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors alien alien.c-types arrays hashtables io kernel -math namespaces opengl opengl.gl opengl.glu sequences strings -vectors combinators math.vectors ui.gadgets colors colors.constants -math.order math.rectangles locals specialized-arrays.float ; +USING: accessors alien alien.c-types arrays hashtables io io.pathnames +kernel math namespaces opengl opengl.gl opengl.glu sequences strings +vectors combinators math.vectors ui.gadgets ui.images colors fry +colors.constants math.order math.rectangles locals +specialized-arrays.float ; IN: ui.render SYMBOL: clip @@ -49,6 +50,10 @@ GENERIC: draw-interior ( gadget interior -- ) GENERIC: draw-boundary ( gadget boundary -- ) +GENERIC: pen-pref-dim ( gadget pen -- dim ) + +M: object pen-pref-dim 2drop { 0 0 } ; + SYMBOL: origin { 0 0 } origin set-global @@ -195,13 +200,62 @@ M: polygon draw-interior [ [ GL_POLYGON 0 ] dip interior-count>> glDrawArrays ] tri ; -CONSTANT: arrow-up { { 3 0 } { 6 6 } { 0 6 } } -CONSTANT: arrow-right { { 0 0 } { 6 3 } { 0 6 } } -CONSTANT: arrow-down { { 0 0 } { 6 0 } { 3 6 } } -CONSTANT: arrow-left { { 0 3 } { 6 0 } { 6 6 } } -CONSTANT: close-box { { 0 0 } { 6 0 } { 6 6 } { 0 6 } } +: theme-image ( name -- image-name ) + "resource:basis/ui/gadgets/theme/" prepend-path ".tiff" append ; -: ( color points -- gadget ) - dup max-dim - [ ] dip >>dim - swap >>interior ; \ No newline at end of file +! Image pen +TUPLE: image-pen image fill? ; + +: ( image -- pen ) f image-pen boa ; + +M: image-pen draw-interior + [ dim>> ] [ [ image>> ] [ fill?>> ] bi ] bi* + [ draw-scaled-image ] [ + [ image-dim [ - 2/ ] 2map ] keep + '[ _ draw-image ] with-translation + ] if ; + +M: image-pen pen-pref-dim nip image>> image-dim ; + +! Tile pen +TUPLE: tile-pen left center right ; + +: ( left center right -- pen ) + tile-pen boa ; + +: >tile-pen< ( pen -- left center right ) + [ left>> ] [ center>> ] [ right>> ] tri ; inline + +M: tile-pen pen-pref-dim + swap [ + >tile-pen< [ image-dim ] tri@ + [ vmax vmax ] [ v+ v+ ] 3bi + ] dip orientation>> set-axis ; + +: compute-tile-xs ( gadget pen -- x1 x2 x3 ) + [ 2drop { 0 0 } ] + [ nip left>> image-dim ] + [ [ dim>> ] [ right>> image-dim ] bi* v- ] + 2tri ; + +: compute-tile-widths ( gadget pen -- w1 w2 w3 ) + [ nip left>> image-dim ] + [ [ dim>> ] [ [ left>> ] [ right>> ] bi [ image-dim ] bi@ ] bi* v+ v- ] + [ nip right>> image-dim ] + 2tri ; + +: render-tile ( tile x width gadget -- ) + [ orientation>> '[ _ v* ] dip ] keep + '[ + _ _ [ dim>> swap ] [ orientation>> ] bi set-axis + swap draw-scaled-image + ] with-translation ; + +M: tile-pen draw-interior ( gadget pen -- ) + { + [ nip >tile-pen< ] + [ compute-tile-xs ] + [ compute-tile-widths ] + [ drop ] + } 2cleave + [ render-tile ] curry tri-curry@ tri-curry* tri* ; \ No newline at end of file