Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
Bevy Cosmic Edit
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Microhacks
Bevy Forks
Bevy Cosmic Edit
Commits
6951ed81
Commit
6951ed81
authored
2 years ago
by
sam edelsten
Browse files
Options
Downloads
Patches
Plain Diff
move text fns to impls
parent
0e5e54f7
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
TODO
+2
-1
2 additions, 1 deletion
TODO
examples/readonly.rs
+7
-9
7 additions, 9 deletions
examples/readonly.rs
src/lib.rs
+82
-70
82 additions, 70 deletions
src/lib.rs
with
91 additions
and
80 deletions
TODO
+
2
−
1
View file @
6951ed81
[
] Friendly get/set text fns on CosmicEditor impl
[
x
] Friendly get/set text fns on CosmicEditor impl
BUGS INTRODUCED:
[ ] Cursor on readonly
[ ] Window resizing broken
Used to redraw on click, now redraws on scroll or drag?
Now drag doesn't work either (tested on readonly, might be that)
BUGS SQUASHED:
...
...
This diff is collapsed.
Click to expand it.
examples/readonly.rs
+
7
−
9
View file @
6951ed81
use
bevy
::{
prelude
::
*
,
window
::
PrimaryWindow
};
use
bevy_cosmic_edit
::{
cosmic_edit_set_text
,
ActiveEditor
,
CosmicAttrs
,
CosmicEditPlugin
,
CosmicEditUiBundle
,
CosmicFontConfig
,
CosmicFontSystem
,
CosmicMetrics
,
CosmicText
,
CosmicTextPosition
,
ReadOnly
,
ActiveEditor
,
CosmicAttrs
,
CosmicEditPlugin
,
CosmicEditUiBundle
,
CosmicFontConfig
,
CosmicFontSystem
,
CosmicMetrics
,
CosmicText
,
CosmicTextPosition
,
ReadOnly
,
};
use
cosmic_text
::
AttrsOwned
;
...
...
@@ -28,8 +28,7 @@ fn setup(
attrs
=
attrs
.family
(
cosmic_text
::
Family
::
Name
(
"Victor Mono"
));
attrs
=
attrs
.color
(
cosmic_text
::
Color
::
rgb
(
0x94
,
0x00
,
0xD3
));
//
let
mut
cosmic_edit
=
CosmicEditUiBundle
{
let
cosmic_edit
=
CosmicEditUiBundle
{
style
:
Style
{
width
:
Val
::
Percent
(
100.
),
height
:
Val
::
Percent
(
100.
),
...
...
@@ -44,21 +43,20 @@ fn setup(
scale_factor
:
primary_window
.scale_factor
()
as
f32
,
},
..
default
()
};
cosmic_edit_set_text
(
}
.set_text
(
CosmicText
::
OneStyle
(
"😀😀😀 x => y
\n
Read only widget"
.to_string
()),
AttrsOwned
::
new
(
attrs
),
&
mut
cosmic_edit
.editor
.0
,
&
mut
font_system
.0
,
);
//
let
mut
id
=
None
;
// Spawn the CosmicEditUiBundle as a child of root
commands
.entity
(
root
)
.with_children
(|
parent
|
{
id
=
Some
(
parent
.spawn
(
cosmic_edit
)
.insert
(
ReadOnly
)
.id
());
});
// Set active editor
commands
.insert_resource
(
ActiveEditor
{
entity
:
id
});
}
...
...
This diff is collapsed.
Click to expand it.
src/lib.rs
+
82
−
70
View file @
6951ed81
...
...
@@ -69,6 +69,73 @@ impl Default for CosmicEditor {
}
}
impl
CosmicEditor
{
pub
fn
set_text
(
&
mut
self
,
text
:
CosmicText
,
attrs
:
AttrsOwned
,
// i'd like to get this automagically but i'm too 3head -bytemunch
font_system
:
&
mut
FontSystem
,
)
->
&
mut
Self
{
let
editor
=
&
mut
self
.0
;
editor
.buffer_mut
()
.lines
.clear
();
match
text
{
CosmicText
::
OneStyle
(
text
)
=>
{
editor
.buffer_mut
()
.set_text
(
font_system
,
text
.as_str
(),
attrs
.as_attrs
(),
Shaping
::
Advanced
,
);
}
// TODO test
CosmicText
::
MultiStyle
(
lines
)
=>
{
for
line
in
lines
{
let
mut
line_text
=
String
::
new
();
let
mut
attrs_list
=
AttrsList
::
new
(
attrs
.as_attrs
());
for
(
text
,
attrs
)
in
line
.iter
()
{
let
start
=
line_text
.len
();
line_text
.push_str
(
text
);
let
end
=
line_text
.len
();
attrs_list
.add_span
(
start
..
end
,
attrs
.as_attrs
());
}
editor
.buffer_mut
()
.lines
.push
(
BufferLine
::
new
(
line_text
,
attrs_list
,
Shaping
::
Advanced
,
));
}
}
}
self
}
/// Retrieves the cosmic text content from an editor.
///
/// # Arguments
///
/// * none, takes the rust magic ref to self
///
/// # Returns
///
/// A `String` containing the cosmic text content.
pub
fn
get_text
(
&
self
)
->
String
{
let
buffer
=
self
.0
.buffer
();
let
mut
text
=
String
::
new
();
let
line_count
=
buffer
.lines
.len
();
for
(
i
,
line
)
in
buffer
.lines
.iter
()
.enumerate
()
{
text
.push_str
(
line
.text
());
if
i
<
line_count
-
1
{
text
.push
(
'\n'
);
}
}
text
}
}
/// Adds the font system to each editor when added
fn
cosmic_editor_builder
(
mut
added_editors
:
Query
<
...
...
@@ -79,7 +146,7 @@ fn cosmic_editor_builder(
)
{
for
(
mut
editor
,
attrs
,
metrics
)
in
added_editors
.iter_mut
()
{
// keep old text if set
let
mut
text
=
get_cosmic_text
(
editor
.0
.buffer
()
);
let
mut
text
=
editor
.get_text
(
);
if
text
.is_empty
()
{
text
=
""
.into
();
...
...
@@ -167,6 +234,18 @@ pub struct CosmicEditUiBundle {
pub
background_image
:
CosmicBackground
,
}
impl
CosmicEditUiBundle
{
pub
fn
set_text
(
mut
self
,
text
:
CosmicText
,
attrs
:
AttrsOwned
,
font_system
:
&
mut
FontSystem
,
)
->
Self
{
self
.editor
.set_text
(
text
,
attrs
,
font_system
);
self
}
}
impl
Default
for
CosmicEditUiBundle
{
fn
default
()
->
Self
{
Self
{
...
...
@@ -301,13 +380,13 @@ fn change_active_editor(
(
Changed
<
Interaction
>
,
With
<
CosmicEditor
>
),
>
,
)
{
for
(
interaction
,
cosmic_
edit
,
entity
)
in
interaction_query
.iter_mut
()
{
for
(
interaction
,
edit
or
,
entity
)
in
interaction_query
.iter_mut
()
{
if
let
Interaction
::
Pressed
=
interaction
{
info!
(
"PRESSED"
);
commands
.insert_resource
(
ActiveEditor
{
entity
:
Some
(
entity
),
});
info!
(
"Widget text: {}"
,
get_cosmic_text
(
cosmic_edit
.0
.buffer
()
));
info!
(
"Widget text: {}"
,
editor
.get_text
(
));
}
}
}
...
...
@@ -406,73 +485,6 @@ pub fn get_node_cursor_pos(
})
}
// Sets text in a CosmicEdit
pub
fn
cosmic_edit_set_text
(
text
:
CosmicText
,
attrs
:
AttrsOwned
,
editor
:
&
mut
Editor
,
font_system
:
&
mut
FontSystem
,
)
{
println!
(
"SETTING TEXT"
);
editor
.buffer_mut
()
.lines
.clear
();
match
text
{
CosmicText
::
OneStyle
(
text
)
=>
{
println!
(
"ONESTYLE {:?}, {:?}"
,
text
,
attrs
);
editor
.buffer_mut
()
.set_text
(
font_system
,
text
.as_str
(),
attrs
.as_attrs
(),
Shaping
::
Advanced
,
);
}
CosmicText
::
MultiStyle
(
lines
)
=>
{
println!
(
"M-M-M-M-MULTI-STYLE"
);
for
line
in
lines
{
let
mut
line_text
=
String
::
new
();
let
mut
attrs_list
=
AttrsList
::
new
(
attrs
.as_attrs
());
for
(
text
,
attrs
)
in
line
.iter
()
{
let
start
=
line_text
.len
();
line_text
.push_str
(
text
);
let
end
=
line_text
.len
();
attrs_list
.add_span
(
start
..
end
,
attrs
.as_attrs
());
}
editor
.buffer_mut
()
.lines
.push
(
BufferLine
::
new
(
line_text
,
attrs_list
,
Shaping
::
Advanced
,
));
}
}
}
}
/// Retrieves the cosmic text content from an editor.
///
/// # Arguments
///
/// * `editor` - A reference to the `Editor` instance containing the text content.
///
/// # Returns
///
/// A `String` containing the cosmic text content.
// TODO put this on the CosmicEdit impl
pub
fn
get_cosmic_text
(
buffer
:
&
Buffer
)
->
String
{
let
mut
text
=
String
::
new
();
let
line_count
=
buffer
.lines
.len
();
for
(
i
,
line
)
in
buffer
.lines
.iter
()
.enumerate
()
{
text
.push_str
(
line
.text
());
if
i
<
line_count
-
1
{
text
.push
(
'\n'
);
}
}
text
}
/// Returns texts from a MultiStyle buffer
pub
fn
get_text_spans
(
buffer
:
&
Buffer
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment