Skip to content

Commit 4b5b0da

Browse files
authored
Merge pull request #17 from kojix2/image
Image
2 parents c24b458 + afe4d43 commit 4b5b0da

9 files changed

Lines changed: 185 additions & 2 deletions

File tree

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,32 @@ Note: Grid Layout does not work as expected on macOS.
440440
</tbody>
441441
</table>
442442

443-
### Timer
443+
### Image (experimental)
444+
445+
<table>
446+
<thead>
447+
<tr>
448+
<th>Example</th>
449+
<th>Ubuntu</th>
450+
<th>Windows</th>
451+
<th>macOS</th>
452+
</tr>
453+
</thead>
454+
<tbody>
455+
<tr>
456+
<td><a href="examples/gallery/area_draw_image.cr">area_draw_image</a></td>
457+
<td><img src="https://raw.githubusercontent.com/kojix2/uing/screenshots/area_draw_image-ubuntu.png" alt="area_draw_image-ubuntu"></td>
458+
<td><img src="https://raw.githubusercontent.com/kojix2/uing/screenshots/area_draw_image-windows.png" alt="area_draw_image-windows"></td>
459+
<td><img src="https://raw.githubusercontent.com/kojix2/uing/screenshots/area_draw_image-macos.png" alt="area_draw_image-macos"></td>
460+
</tr>
461+
<tr>
462+
<td><a href="examples/gallery/image_viewer.cr">image_viewer</a></td>
463+
<td><img src="https://raw.githubusercontent.com/kojix2/uing/screenshots/image_viewer-ubuntu.png" alt="image_viewer-ubuntu"></td>
464+
<td><img src="https://raw.githubusercontent.com/kojix2/uing/screenshots/image_viewer-windows.png" alt="image_viewer-windows"></td>
465+
<td><img src="https://raw.githubusercontent.com/kojix2/uing/screenshots/image_viewer-macos.png" alt="image_viewer-macos"></td>
466+
</tr>
467+
</tbody>
468+
</table>
444469

445470
## API Levels
446471

download.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require "compress/zip"
22
require "file_utils"
33

4-
COMMIT_HASH = ENV["LIBUI_NG_COMMIT_HASH"]? || "b44b29e"
4+
COMMIT_HASH = ENV["LIBUI_NG_COMMIT_HASH"]? || "d846eac-experimental"
55

66
# Path constants
77
BUILD_DIR = "builddir"
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require "../../src/uing"
2+
require "stumpy_png"
3+
4+
fname = "#{__DIR__}/crys.png"
5+
canvas = StumpyPNG.read(fname)
6+
width = canvas.width.to_i32
7+
height = canvas.height.to_i32
8+
9+
pixels = Bytes.new(width * height * 4)
10+
(0...height).each do |y|
11+
(0...width).each do |x|
12+
offset = (y * width + x) * 4
13+
r, g, b, a = canvas[x, y].to_rgba
14+
pixels[offset] = r
15+
pixels[offset + 1] = g
16+
pixels[offset + 2] = b
17+
pixels[offset + 3] = a || 255_u8
18+
end
19+
end
20+
21+
UIng.init
22+
23+
image = UIng::Image.new(width, height)
24+
image.append(pixels, width, height, width * 4)
25+
26+
window = UIng::Window.new("Draw Image Example", 400, 400, margined: true)
27+
window.on_closing do
28+
UIng.quit
29+
true
30+
end
31+
32+
area_handler = UIng::Area::Handler.new do |header|
33+
draw do |area, params|
34+
ctx = params.context
35+
white_brush = UIng::Area::Draw::Brush.new(:solid, 1.0, 1.0, 1.0, 1.0)
36+
ctx.fill_path(white_brush) do |path|
37+
path.add_rectangle(0, 0, params.area_width, params.area_height)
38+
end
39+
begin
40+
ctx.draw_image(image, 10, 10, 100, 100)
41+
ctx.draw_image(image, 160, 10, 200, 100)
42+
ctx.draw_image(image, 10, 160, 100, 200)
43+
ctx.draw_image(image, 160, 160, 200, 200)
44+
rescue ex
45+
UIng.handle_callback_error(ex, "draw_image")
46+
end
47+
end
48+
end
49+
50+
area = UIng::Area.new(area_handler)
51+
box = UIng::Box.new(:horizontal)
52+
box.append(area, stretchy: true)
53+
window.set_child(box)
54+
window.show
55+
56+
UIng.main
57+
58+
image.free
59+
UIng.uninit
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require "../../src/uing"
2+
require "http/client"
3+
require "stumpy_png"
4+
5+
fname = "#{__DIR__}/crys.png"
6+
canvas = StumpyPNG.read(fname)
7+
width = canvas.width.to_i32
8+
height = canvas.height.to_i32
9+
10+
pixels = Bytes.new(width * height * 4)
11+
(0...height).each do |y|
12+
(0...width).each do |x|
13+
offset = (y * width + x) * 4
14+
r, g, b, a = canvas[x, y].to_rgba
15+
pixels[offset] = r
16+
pixels[offset + 1] = g
17+
pixels[offset + 2] = b
18+
pixels[offset + 3] = a || 255_u8
19+
end
20+
end
21+
22+
UIng.init
23+
24+
window = UIng::Window.new("ImageView Example", 300, 200, margined: true)
25+
window.on_closing do
26+
UIng.quit
27+
true
28+
end
29+
30+
vbox = UIng::Box.new(:vertical)
31+
32+
image = UIng::Image.new(width, height)
33+
image.append(pixels, width, height, width * 4)
34+
image_view = UIng::ImageView.new(image, :fit)
35+
image.free
36+
37+
label = UIng::Label.new(fname)
38+
39+
vbox.append(image_view, stretchy: true)
40+
vbox.append(label)
41+
42+
window.set_child(vbox)
43+
window.show
44+
45+
UIng.main
46+
UIng.uninit

examples/gallery/crys.png

72.4 KB
Loading

shard.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@ authors:
77
scripts:
88
postinstall: crystal run download.cr
99

10+
11+
development_dependencies:
12+
stumpy_png:
13+
github: stumpycr/stumpy_png
14+
version: "~> 5.0"
15+
1016
license: MIT

src/uing/area/area/draw/context.cr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ module UIng
133133
LibUI.draw_text(@ref_ptr, text_layout.to_unsafe, x, y)
134134
end
135135

136+
def draw_image(img : UIng::Image, x : Number, y : Number, width : Number, height : Number) : Nil
137+
raise ArgumentError.new("Width must be positive") if width <= 0
138+
raise ArgumentError.new("Height must be positive") if height <= 0
139+
140+
LibUI.draw_image(@ref_ptr, img.to_unsafe, x.to_f64, y.to_f64, width.to_f64, height.to_f64)
141+
end
142+
136143
def to_unsafe
137144
@ref_ptr
138145
end

src/uing/image_view.cr

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require "./control"
2+
3+
module UIng
4+
class ImageView < Control
5+
enum ContentMode
6+
Center
7+
Fit
8+
end
9+
10+
block_constructor
11+
12+
def initialize
13+
@ref_ptr = LibUI.new_image_view
14+
end
15+
16+
def initialize(image : Image, mode : ContentMode = ContentMode::Center)
17+
@ref_ptr = LibUI.new_image_view
18+
LibUI.image_view_set_image(@ref_ptr, image.to_unsafe)
19+
LibUI.image_view_set_content_mode(@ref_ptr, mode)
20+
end
21+
22+
def image=(image : Image)
23+
LibUI.image_view_set_image(@ref_ptr, image.to_unsafe)
24+
end
25+
26+
def content_mode=(mode : ContentMode)
27+
LibUI.image_view_set_content_mode(@ref_ptr, mode)
28+
end
29+
30+
def to_unsafe
31+
@ref_ptr
32+
end
33+
end
34+
end

src/uing/lib_ui/lib_ui.cr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ module UIng
339339
fun draw_free_text_layout = uiDrawFreeTextLayout(tl : Pointer(DrawTextLayout))
340340
fun draw_text = uiDrawText(c : Pointer(DrawContext), tl : Pointer(DrawTextLayout), x : LibC::Double, y : LibC::Double)
341341
fun draw_text_layout_extents = uiDrawTextLayoutExtents(tl : Pointer(DrawTextLayout), width : Pointer(LibC::Double), height : Pointer(LibC::Double))
342+
fun draw_image = uiDrawImage(c : Pointer(DrawContext), img : Pointer(Image), x : LibC::Double, y : LibC::Double, width : LibC::Double, height : LibC::Double)
342343
alias FontButton = Void
343344
fun font_button_font = uiFontButtonFont(b : Pointer(FontButton), desc : Pointer(FontDescriptor))
344345
fun font_button_on_changed = uiFontButtonOnChanged(b : Pointer(FontButton), f : (Pointer(FontButton), Pointer(Void) -> Void), data : Pointer(Void))
@@ -413,5 +414,10 @@ module UIng
413414
fun table_get_selection = uiTableGetSelection(t : Pointer(Table)) : Pointer(TableSelection)
414415
fun table_set_selection = uiTableSetSelection(t : Pointer(Table), sel : Pointer(TableSelection))
415416
fun free_table_selection = uiFreeTableSelection(s : Pointer(TableSelection))
417+
418+
alias ImageView = Void
419+
fun new_image_view = uiNewImageView : Pointer(ImageView)
420+
fun image_view_set_image = uiImageViewSetImage(iv : Pointer(ImageView), image : Pointer(Image))
421+
fun image_view_set_content_mode = uiImageViewSetContentMode(iv : Pointer(ImageView), mode : UIng::ImageView::ContentMode)
416422
end
417423
end

0 commit comments

Comments
 (0)