Skip to content

Commit

Permalink
Remove the textures, now it's Elements only. (#52)
Browse files Browse the repository at this point in the history
Erik Corry authored Dec 6, 2023
1 parent ed76508 commit c19dcfa
Showing 36 changed files with 103 additions and 4,283 deletions.
28 changes: 12 additions & 16 deletions src/common.toit
Original file line number Diff line number Diff line change
@@ -2,27 +2,26 @@
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.
// Common things between textures and elements.
// TODO: Absorb this into pixel_display.toit now that textures are gone.
import font show Font
import bitmap show ORIENTATION_0 ORIENTATION_90 ORIENTATION_180 ORIENTATION_270

import .element show Element
import .style

/**
A display or a window within a display.
You can add and remove texture objects to a Window. They will be drawn
in the order they were added, where the first textures are at the back
and are overwritten by textures added later.
You can add and remove element objects to a Window. They will be drawn
in the order they were added, where the first elements are at the back
and are overwritten by elements added later.
*/
interface Window:
add element/ElementOrTexture_ -> none
remove element/ElementOrTexture_ -> none
add element/Element -> none
remove element/Element -> none
remove_all -> none

// Called by elements that have been added to this.
child_invalidated x/int y/int w/int h/int -> none

// Called by elements that have been added to this.
child_invalidated_element x/int y/int w/int h/int -> none

@@ -47,11 +46,9 @@ abstract class ElementOrTexture_:
abstract invalidate -> none

abstract class Canvas:
width_ / int // Used by both Textures and Elements.
height_ / int // Only used by Textures.
x_offset_ / int := 0 // Only used by Textures.
y_offset_ / int := 0 // Only used by Textures.
transform / Transform? := null // Only used by Elements.
width_ / int
height_ / int
transform / Transform? := null

constructor .width_ .height_:

@@ -211,8 +208,7 @@ class Transform:
a horizontal line (if you specify the $y coordinate) or a vertical line
(if you specify the $x coordinate).
You cannot specify both $x and $y. Not all textures support
reflected transforms. In particular, text and icons cannot be reflected.
You cannot specify both $x and $y. We do not support reflected transforms.
Most of this library is integer-only, but for this operation you may need
to use a half-pixel line depending on whether the thing you want to reflect
@@ -271,7 +267,7 @@ class TextExtent_:
// Gets the graphical extent of a string nominally positioned at (0, 0),
// Where the nominal position of the text is relative to the letters depends
// on the alignment, but it is always somewhere on the textual baseline.
// Some confusion is caused here by the fact that the y-axis of textures
// Some confusion is caused here by the fact that the y-axis of elements
// grows towards the bottom, whereas the y-axis of fonts grows towards the
// top.
constructor text font alignment:
3 changes: 0 additions & 3 deletions src/element.toit
Original file line number Diff line number Diff line change
@@ -74,9 +74,6 @@ abstract class Element extends ElementOrTexture_ implements Window:
element.invalidate
element.change_tracker = null

child_invalidated x/int y/int w/int h/int -> none:
unreachable // This is only for textures, but we don't allow those.
remove_all -> none:
children.do:
it.invalidate
186 changes: 0 additions & 186 deletions src/four_gray.toit
Original file line number Diff line number Diff line change
@@ -12,7 +12,6 @@ import font show Font
import icons show Icon
import .common
import .pixel_display show FourGrayPixelDisplay // For the doc comment.
import .texture
import .two_bit_texture
import .two_bit_texture as two_bit

@@ -41,188 +40,3 @@ class Canvas_ extends two_bit.Canvas_:
result := Canvas_ width_ height_
result.transform = transform
return result

class FilledRectangle extends TwoBitFilledRectangle_:
constructor color x/int y/int w/int h/int transform/Transform:
super color x y w h transform

/// A line from $x1,$y1 to $x2,$y2. The line must be horizontal or vertical.
constructor.line color x1/int y1/int x2/int y2/int transform/Transform:
return FilledRectangle_.line_ x1 y1 x2 y2: | x y w h |
FilledRectangle color x y w h transform

class TextTexture extends TwoBitTextTexture_:
/**
The coordinates given here to the constructor (and move_to) are the bottom
left of the first letter in the string (for left alignment). Once the
string has been rotated and aligned, and overhanging letter shapes have
been taken into account, the top left of the bounding box (properties $x,
$y, inherited from $SizedTexture) reflects the actual top left position
in the coordinate system of the transform. In the coordinates of the
display the getters $display_x, $display_y, $display_w and $display_h
are available.
*/
constructor text_x/int text_y/int transform/Transform alignment/int text/string font color:
super text_x text_y transform alignment text font color

class IconTexture extends TwoBitTextTexture_:
constructor icon_x/int icon_y/int transform/Transform alignment/int icon/Icon font/Font color/int:
super icon_x icon_y transform alignment icon.stringify icon.font_ color

icon= new_icon/Icon -> none:
text = new_icon.stringify
font = new_icon.font_

/**
A texture that contains an uncompressed 2-color image.
Initially all pixels are transparent, but pixels can be given the color
with $set_pixel.
*/
class BitmapTexture extends TwoBitBitmapTexture_:
constructor x/int y/int w/int h/int transform/Transform color/int:
super x y w h transform color

/**
A two color bitmap texture where foreground and background pixels in the
texture are both drawn.
Initially all pixels have the background color.
Use $set_pixel to paint with the foreground, and $clear_pixel to paint with
the background.
*/
class OpaqueBitmapTexture extends TwoBitOpaqueBitmapTexture_:

constructor x/int y/int w/int h/int transform/Transform foreground_color/int background_color:
super x y w h transform foreground_color background_color

/// A four-color pixmap texture.
/// Use $set_all_pixels to set a background color and $set_pixel to draw.
class OpaquePixmapTexture extends BitmapTextureBase_:
bytes_/ByteArray ::= ?
bytes_2_/ByteArray ::= ?

constructor x/int y/int w/int h/int transform/Transform initial_color/int=0:
bytes_per_plane := h * ((w + 7) >> 3) // Divide by 8, rounding up.
bytes_ = ByteArray bytes_per_plane
bytes_2_ = ByteArray bytes_per_plane
super x y w h transform
set_all_pixels initial_color

pixel_color x/int y/int -> int:
index_and_mask_ x y: | index bit |
lo := (bytes_[index] & bit) == 0 ? 0 : 1
hi := (bytes_2_[index] & bit) == 0 ? 0 : 2
return lo + hi
unreachable

set_pixel x/int y/int color/int -> none:
index_and_mask_ x y: | index bit |
if color & 1 == 0:
bytes_[index] &= bit ^ 0b1111_1111
else:
bytes_[index] |= bit
if color & 2 == 0:
bytes_2_[index] &= bit ^ 0b1111_1111
else:
bytes_2_[index] |= bit

set_all_pixels color/int -> none:
bitmap_zap bytes_ color & 1
bitmap_zap bytes_2_ (color & 2) >> 1

write2_ canvas/Canvas_:
transform_.xywh x_ y_ w_ h_: | x2 y2 w2 h2 |
x := x2 - canvas.x_offset_
y := y2 - canvas.y_offset_
// Zero out the area of the Pixmap.
bitmap_rectangle x y 0 w2 h2 canvas.plane_0_ canvas.width_
bitmap_rectangle x y 0 w2 h2 canvas.plane_1_ canvas.width_
super canvas // Calls draw_
draw_ bx by orientation canvas/Canvas_:
// The area was already zeroed, add in the 1s as needed.
bitmap_draw_bitmap bx by 1 orientation bytes_ 0 w canvas.plane_0_ canvas.width_ false
bitmap_draw_bitmap bx by 1 orientation bytes_2_ 0 w canvas.plane_1_ canvas.width_ false

// A texture backed by a P4 (binary two-level) PBM file. The white areas
// (zeros) are rendered transparent and the black areas (ones) are rendered in
// an arbitrary color.
class PbmTexture extends PbmTexture_:
// The byte array passed in must be a valid binary-mode (P4) PBM file.
// If $bytes is a literal containing constants then it is used directly
// from flash. However if the pixel drawing methods on this are used then
// $bytes is moved to RAM and modified. This could cause an out-of-memory
// on very large PBM files.
constructor x/int y/int transform/Transform color/int bytes/ByteArray:
super x y transform color bytes

class BarCodeEan13 extends TwoBitBarCodeEan13_:
constructor code/string x/int y/int transform/Transform:
super code x y transform BLACK WHITE

/**
A rectangular window with a fixed width colored border.
The border is subtracted from the visible area inside the window.
*/
class SimpleWindow extends TwoBitSimpleWindow_:
constructor x/int y/int w/int h/int transform/Transform border_width/int border_color/int background_color/int:
super x y w h transform border_width border_color background_color

class RoundedCornerWindow extends RoundedCornerWindow_:
background_color := ?

constructor x y w h transform corner_radius .background_color:
super x y w h transform corner_radius

make_alpha_map_ canvas/Canvas_ padding:
return ByteArray ((canvas.width_ + padding) * (canvas.height_ + padding)) >> 3

make_opaque_ x y w h map map_width --frame/bool:
assert: not frame
bytemap_rectangle x y 0xff w h map map_width

set_opacity_ x y opacity map map_width --frame/bool:
assert: not frame
if 0 <= x < map_width:
y_offset := y * map_width
if 0 <= y_offset < map.size:
map[x + y_offset] = opacity

draw_background canvas/Canvas_:
bytemap_zap canvas.plane_0_ (background_color & 1)
bytemap_zap canvas.plane_1_ (background_color & 2) >> 1

draw_frame canvas/Canvas_:
throw "UNREACHABLE"

class DropShadowWindow extends DropShadowWindow_:
background_color := ?
max_shadow_opacity_ := ?

constructor x y w h transform .background_color --corner_radius=5 --blur_radius=5 --drop_distance_x=10 --drop_distance_y=10 --shadow_opacity_percent=25:
// Scale the 0-100% opacity percentage to cover the 8 bit unsigned integer
// range 0-255.
max_shadow_opacity_ = (shadow_opacity_percent * 2.5500001).to_int
super x y w h transform corner_radius blur_radius drop_distance_x drop_distance_y

make_alpha_map_ canvas/Canvas_ padding:
return ByteArray (canvas.width_ + padding) * (canvas.height_ + padding)

make_opaque_ x y w h map map_width --frame/bool:
bytemap_rectangle x y (frame ? max_shadow_opacity_ : 255) w h map map_width

set_opacity_ x y opacity map map_width --frame/bool:
if 0 <= x < map_width:
y_offset := y * map_width
if 0 <= y_offset < map.size:
if frame:
map[x + y_offset] = (opacity * max_shadow_opacity_) >> 8
else:
map[x + y_offset] = opacity

draw_background canvas/Canvas_:
bytemap_zap canvas.plane_0_ (background_color & 1)
bytemap_zap canvas.plane_1_ (background_color & 2) >> 1

draw_frame canvas/Canvas_:
bytemap_zap canvas.plane_0_ 0
bytemap_zap canvas.plane_1_ 0
Loading

0 comments on commit c19dcfa

Please sign in to comment.