Skip to content

Commit

Permalink
Improved responsiveness. Updated screenshot. Added rounding.
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilr612 committed Jul 19, 2024
1 parent 2908fd6 commit bffb43f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn main() {
```
## Screenshot

The screenshot of another example.
The screenshot of another example, showcasing various widgets.

![Screenshot of an example program](screenshot.png)

Expand All @@ -57,4 +57,4 @@ The following features will not be supported in this integration:
1. Rendering arbitrary meshes.
2. [Paint callbacks](https://docs.rs/epaint/0.28.1/epaint/struct.PaintCallback.html).

The primary reason behind this is that this integration does not rely on egui to tessellate its entire UI-mesh, but rather traverses the output shape tree and calls corresponding raylib functions on a draw handle. If necessary, these features can be obtained by using egui's built-in tessellation functionality to generate primitives that can be rendered directly. This approach was not chosen to allow the ui to be rendered on any draw handle that supports clipping.
The primary reason behind this is that this integration does not rely on egui to tessellate its entire UI-mesh, but rather traverses the output shape tree and calls corresponding raylib functions on a draw handle. If necessary, these features can be obtained by using egui's built-in tessellation functionality to generate primitives that can be rendered directly. This approach was not chosen to allow the ui to be rendered on any draw handle that supports clipping.
Binary file modified screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 3 additions & 4 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ fn get_mouse_input(
events: &mut Vec<Event>,
pixels_per_point: f32,
modifiers: Modifiers,
ctx: &egui::Context
) {
let mouse_delta = rl.get_mouse_delta().scale_by(1.0 / pixels_per_point);
let mouse_position = rl.get_mouse_position().scale_by(1.0 / pixels_per_point);

if mouse_delta.x > 0.0 || mouse_delta.y > 0.0 {
if mouse_delta.x > 0.0 || mouse_delta.y > 0.0 || ctx.wants_pointer_input() {
events.push(Event::MouseMoved(Vec2::new(mouse_delta.x, mouse_delta.y)));
events.push(Event::PointerMoved(Pos2::new(
mouse_position.x,
Expand Down Expand Up @@ -207,9 +208,7 @@ pub fn gather_input(opt: &InputOptions, ctx: &egui::Context, rl: &mut RaylibHand
}
}

// if ctx.wants_pointer_input() {
get_mouse_input(rl, &mut events, pixels_per_point, modifiers);
// }
get_mouse_input(rl, &mut events, pixels_per_point, modifiers, ctx);

let dropped_files = if rl.is_file_dropped() {
rl.load_dropped_files()
Expand Down
40 changes: 24 additions & 16 deletions src/paint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use egui::{
ahash::HashMap, epaint::ImageDelta, output::OutputEvent, Context, FullOutput, OpenUrl,
RawInput, TextureId,
};
use egui::{Mesh, Vec2};
use egui::{Mesh, Rounding, Vec2};
use raylib::color::Color;
use raylib::drawing::RaylibScissorModeExt;
use raylib::ffi::Rectangle;
Expand Down Expand Up @@ -288,24 +288,32 @@ impl Painter {
};
let fill_color = rs.fill.convert();
let stroke_color = rs.stroke.color.convert();
d.draw_rectangle_rec(rrect2, stroke_color);

if rs.uv == egui::Rect::ZERO {
// No texture here.
d.draw_rectangle_rec(rrect, fill_color);
} else {
// Draw textured rectangle.
if let Some(texture) = self.textures.get(&rs.fill_texture_id) {
let source_rec = Rectangle {
x: rs.uv.min.x * texture.width as f32,
y: rs.uv.max.y * texture.height as f32,
width: rs.uv.width(),
height: rs.uv.height()
};
d.draw_texture_pro(texture, source_rec, rrect, Vector2::zero(), 0.0, fill_color)
if rs.rounding == Rounding::ZERO {
d.draw_rectangle_rec(rrect2, stroke_color);
if rs.uv == egui::Rect::ZERO {
// No texture here.
d.draw_rectangle_rec(rrect, fill_color);
} else {
d.draw_rectangle_rec(rrect, fill_color)
// Draw textured rectangle.
if let Some(texture) = self.textures.get(&rs.fill_texture_id) {
let source_rec = Rectangle {
x: rs.uv.min.x * texture.width as f32,
y: rs.uv.max.y * texture.height as f32,
width: rs.uv.width(),
height: rs.uv.height()
};
d.draw_texture_pro(texture, source_rec, rrect, Vector2::zero(), 0.0, fill_color)
} else {
d.draw_rectangle_rec(rrect, fill_color)
}
}
} else {
// Can't draw textures on rounded rectangles.
// Raylib roundedness is the ratio between the radius and the smallest dimension.
let roundness = rs.rounding.ne.max(rs.rounding.nw).max(rs.rounding.se).max(rs.rounding.sw) * pxpp / rrect.width.min(rrect.height);
d.draw_rectangle_rounded(rrect2, roundness, 4, stroke_color);
d.draw_rectangle_rounded(rrect, roundness, 4, fill_color);
}
},

Expand Down
5 changes: 3 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use egui::{Color32, Context, Label, RichText, Visuals};
use egui::{Color32, Context, Label, RichText, Rounding, Visuals};
use raylib::prelude::{Color, RaylibDraw};

use crate::{input::InputOptions, RlEgui};
Expand Down Expand Up @@ -209,11 +209,12 @@ fn it_works() {
ctx.set_visuals(Visuals {
override_text_color: Some(Color32::WHITE),
hyperlink_color: Color32::BLUE,
window_rounding: Rounding::ZERO,
..Visuals::dark()
});

let inopt = InputOptions {
native_pixels_per_point: 1.25,
native_pixels_per_point: 1.3,
..Default::default()
};

Expand Down

0 comments on commit bffb43f

Please sign in to comment.