-
-
Notifications
You must be signed in to change notification settings - Fork 197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Window.takeScreenshot API #225
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e763bbe
Integrate Window.takeScreenshot into examples
OhadRau d8be26e
Add Window.takeScreenshot
OhadRau 6575d1d
Change screen capture example
OhadRau f4913b4
Merge in new styles
OhadRau 3ad4e55
Fix formatting
OhadRau a7c682b
Merge branch 'master' into take-screenshot
bryphe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
open Revery.UI; | ||
open Revery.UI.Components; | ||
open Revery.Core; | ||
|
||
let backgroundColor = Color.hex("#212733"); | ||
let activeBackgroundColor = Color.hex("#2E3440"); | ||
let inactiveBackgroundColor = Color.hex("#272d39"); | ||
let selectionHighlight = Color.hex("#90f7ff"); | ||
|
||
module ActionButton = { | ||
let component = React.component("ActionButton"); | ||
|
||
let make = (~name, ~onClick, ()) => | ||
component((_slots: React.Hooks.empty) => { | ||
let wrapperStyle = | ||
Style.[ | ||
backgroundColor(selectionHighlight), | ||
border(~width=4, ~color=activeBackgroundColor), | ||
]; | ||
let textHeaderStyle = | ||
Style.[ | ||
color(Colors.black), | ||
fontFamily("Roboto-Regular.ttf"), | ||
fontSize(14), | ||
margin(16), | ||
]; | ||
<Clickable style=wrapperStyle onClick> | ||
<Text style=textHeaderStyle text=name /> | ||
</Clickable>; | ||
}); | ||
|
||
let createElement = (~children as _, ~name, ~onClick, ()) => | ||
React.element(make(~name, ~onClick, ())); | ||
}; | ||
|
||
module CaptureArea = { | ||
let component = React.component("Capture Area"); | ||
|
||
let make = w => | ||
component(slots => { | ||
let (count, setCount, slots) = React.Hooks.state(0, slots); | ||
let (file, setFile, _slots: React.Hooks.empty) = | ||
React.Hooks.state(None, slots); | ||
|
||
let capture = () => { | ||
let exed = Environment.getExecutingDirectory(); | ||
let filename = Printf.sprintf("Scrot_%d.tga", count); | ||
let fullname = exed ++ filename; | ||
Window.takeScreenshot(w, fullname); | ||
setCount(count + 1); | ||
setFile(Some(filename)); | ||
}; | ||
|
||
let viewStyle = | ||
Style.[ | ||
position(`Absolute), | ||
left(0), | ||
right(0), | ||
top(0), | ||
bottom(0), | ||
flexDirection(`Column), | ||
]; | ||
|
||
let imageStyle = Style.[width(400), height(300)]; | ||
|
||
<View style=viewStyle> | ||
<ActionButton name="Take a screenshot!" onClick=capture /> | ||
{switch (file) { | ||
| None => <View /> | ||
| Some(src) => <Image style=imageStyle src /> | ||
}} | ||
</View>; | ||
}); | ||
|
||
let createElement = (~w, ~children as _, ()) => React.element(make(w)); | ||
}; | ||
|
||
let render = w => <CaptureArea w />; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -323,6 +323,26 @@ let getDevicePixelRatio = (w: t) => { | |
/. float_of_int(windowSizeInScreenCoordinates.width); | ||
}; | ||
|
||
let takeScreenshot = (w: t, filename: string) => { | ||
open Glfw; | ||
|
||
let width = w.framebufferWidth; | ||
let height = w.framebufferHeight; | ||
|
||
let image = Image.create(~width, ~height, ~numChannels=4, ~channelSize=1); | ||
let buffer = Image.getBuffer(image); | ||
|
||
/* WebGL is weird in that we can't capture with glReadPixels during | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the comment here! I was wondering why we needed the extra |
||
a render operation. Instead, we want to wait till it's over (we | ||
can force this by triggering a new render) and then taking the | ||
screenshot */ | ||
render(w); | ||
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer); | ||
|
||
Image.save(image, filename); | ||
Image.destroy(image); | ||
}; | ||
|
||
let destroyWindow = (w: t) => Glfw.glfwDestroyWindow(w.glfwWindow); | ||
|
||
let shouldClose = (w: t) => Glfw.glfwWindowShouldClose(w.glfwWindow); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't it be useful to put the Screen Capture button at the bottom of the screen? For example with something like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, do you want me to go back and do that? Right now this is setup to be in its own tab as bryan pointed out. This is a little weird because you can't get screenshots of everything but it's more of a proof of concept than anything else so I'm not too concerned about it. One of the advantages of having it in its own tab is that we can display the image right there so we can really show it in action. @tcoopman
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm OK with it is, for now - what I'd like to do with the example app is host it on a website, and show the example code for the tab side-by-side with the selected example.
For the screenshot example - it'd be nice to see how easy it is to implement this behavior w/ that code snippet alongside. We can always revisit down the road though if we want to shuffle it around.